$(窗口).unload不点火 [英] $(window).unload is not firing
问题描述
我工作的一个MVC剃刀项目和使用jquery我想执行,当用户正在抛弃特定页面的操作方法。
I'm working on an MVC Razor project and by using jquery I want to execute an action method when the user is abandoning a particular page.
该_layout页面已经加载了jQuery库,它有一个脚本的一节。
该页面有如下code:
The _layout page is already loading the jquery library and it has a scripts section. The page has the following code:
@section scripts{
<script type="text/javascript">
$(window).unload(function () {
alert("Handler for .unload() was called.");
});
</script>
}
如果我看查看源文件,我可以看到,该脚本被正确添加页眉中。
If I look at the view source I can see that the script is being added correctly in the page header.
现在,当我选择一个书签从页面导航离开我从来没有看到预期的警告。
Now when I navigate away from the page by selecting a bookmark I never see the expected alert.
我是什么失踪?
感谢。
推荐答案
如果您尝试其实一些浏览器,如谷歌浏览器可能会阻止警报
在窗口卸载
。作为一个用户,我喜欢这个功能。警报每次尝试导航离开页面吮吸:
Actually some browsers such as Google Chrome might block if you attempt to alert
in a window unload
. As a user I like this feature. Alerting everytime you try to navigate away from a page sucks:
替换警报一的console.log
或别的东西,更少侵入到用户和事件将被称为愉快
Replace the alert with a console.log
or something else less intrusive to the user and the event will be happily called.
您可能还需要签出 onbeforeunload 事件。
You might also want to checkout the onbeforeunload event.
这篇关于$(窗口).unload不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!