javascriptvisual-studio-2013onbeforeunloadbrowser-link

onbeforeunload doesn't work from Visual Studio 2013


Create a web project in Visual Studio 2013, remove all scripts (which could interfere) and add this:

<script type="text/javascript" src="~/Scripts/jquery-1.8.2.min.js"></script> <!-- Note that the version might be different -->
<script type="text/javascript">
    jQuery(window).bind('beforeunload', function (e) {
        var message = "Why are you leaving?";
        e.returnValue = message;
        return message;
    });
</script>

The onbeforeunload event won't fire. However if you open this with Visual Studio 2012 it will work (note that to open it with Visual Studio 2012 you shouldn't use ASP.NET MVC 5).


Solution

  • Browser Link (one of the new features of Visual Studio 2013) is replacing the original onbeforeunload event. Here this is reported for Firefox although this also happens in other browsers.

    If you want to work with onbeforeunload event you should disable Browser Link.