在 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
)。