在 Visual Studio 2013 中创建一个 Web 项目,删除所有脚本(可能会干扰)并添加以下内容:
<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>
onbeforeunload
事件不会触发。但是,如果您使用 Visual Studio 2012 打开它,它将可以工作(请注意,要使用 Visual Studio 2012 打开它,您不应使用 ASP.NET MVC 5
)。
最佳答案
Browser Link (one of the new features of Visual Studio 2013)正在替换原来的 onbeforeunload
事件。 Here this is reported for Firefox尽管这也发生在其他浏览器中。
如果您想使用 onbeforeunload
事件,您应该 disable Browser Link .
关于javascript - onbeforeunload 在 Visual Studio 2013 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047508/