Javascript:IE 和 Chrome 中的 onBeforeUnload() 行为
我有这段代码:
window.onbeforeunload = function(){ doSomething(); return "Are you sure you want to leave"; }
在 Firefox 中,当用户离开页面时,首先执行 doSomething() ,然后弹出警报。 就像在 IE 和 Chrome 中一样,警报会首先弹出,然后如果用户决定保留 doSomething()
被处决。
有没有办法让 IE 和 Chrome 表现得像 Firefox。我想在显示弹出窗口之前执行 doSomething()
。
I have this piece of code:
window.onbeforeunload = function(){ doSomething(); return "Are you sure you want to leave"; }
In Firefox when user leaves the page, doSomething()
gets executed first and then the alert pops up.
where as in IE and Chrome the alert pops up first and then if the user decides to stay doSomething()
gets executed.
Is there a way to make IE and Chrome behave like Firefox. I want to execute doSomething()
before showing the pop up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用什么版本的 Firefox?据我所知,除了传递要包含在弹出的确认对话框中的文本之外,所有浏览器都不应该让您在 onBeforeUnload 上执行任何操作 - 因此您无法阻止用户离开您的网站。
您不应依赖此事件来确保用户离开页面时发生任何事情。如果这很重要,请将点击事件处理程序分配给可以导航离开页面的所有内容(您仍然会失去浏览器导航按钮和地址栏更改)
What version of Firefox are you using? As far as I know none of the browsers are supposed to let you do anything on onBeforeUnload other than pass text to be included in the confirm dialog that pops up - so you can't prevent the user from leaving your site.
You shouldn't rely on this event to ensure anything happens when the user leaves your page. If it's that important assign a click event handler to everything that can navigate away from your page (you'll still lose out on the browser navigate buttons and address bar changes)