window.onbeforeunload 在 Chrome 中不起作用
这是我用于 window.onbeforeunload
的代码,
<head>
<script>
window.onbeforeunload = func;
function func()
{
var request = new XMLHttpRequest();
request.open("POST", "exit.php", true);
request.onreadystatechange = stateChanged;
request.send(null);
}
function stateChanged()
{
if (request.readyState == 4 || request.readyState == "complete")
alert("Succes!");
}
</script>
</head>
它适用于 IE 和 Mozilla,但不适用于 Chrome >......请帮忙...... 提前致谢.....
This is the code which i used for window.onbeforeunload
<head>
<script>
window.onbeforeunload = func;
function func()
{
var request = new XMLHttpRequest();
request.open("POST", "exit.php", true);
request.onreadystatechange = stateChanged;
request.send(null);
}
function stateChanged()
{
if (request.readyState == 4 || request.readyState == "complete")
alert("Succes!");
}
</script>
</head>
this works with IE and Mozilla but does not work with Chrome..... please help......
thanks in advance.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
看来在最近版本的 Chrome 中,onbeforeunload 唯一能做的就是设置警告消息。
会工作的。 Chrome UPDATE 似乎会忽略函数中的其他代码
:从 Chrome V51 开始,返回的字符串将被忽略,并显示默认消息。
It seems that the only thing you can do with onbeforeunload in recent version of Chrome is to set the warning message.
Will work. Other code in the function seems to be ignored by Chrome
UPDATE: As of Chrome V51, the returned string will be ignored and a default message shown instead.
我知道我迟到了,但我很困惑为什么我的自定义 beforeunload 消息在 Chrome 中不起作用并且正在阅读此内容。因此,如果其他人也这样做,Chrome 从版本 51 开始不再支持 beforeunload 上的自定义消息。显然这是因为该功能已被各种骗局滥用。相反,您会收到一条预定义的 Chrome 消息,该消息可能适合也可能不适合您的目的。更多详细信息,请访问:
https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove-custom-messages-in-onbeforeload-dialogs
个人认为不认为他们选择的消息是一个很好的消息,因为它提到离开网站,并且 onbeforeunload 最常见的合法用途之一是用于脏标志处理/检查网络表单,所以它不是一个很好的措辞,因为很多当用户仍然在您的网站上时,只是错误地单击了取消或重新加载按钮。
Know I'm late to this, but was scratching my head why my custom beforeunload message wasn't working in Chrome and was reading this. So in case anyone else does the same, Chrome from Version 51 onwards no longer supports custom messages on beforeunload. Apparently it's because the feature has been misused by various scams. Instead you get a predefined Chrome message which may or may not suit your purposes. More details at:
https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove-custom-messages-in-onbeforeload-dialogs
Personally do not think the message they've chosen is a great one as it mentions leaving the site and one of the most common legitimate uses for onbeforeunload is for dirty flag processing/checking on a web form so it's not a great wording as a lot of the time the user will still be on your site, just have clicked the cancel or reload button by mistake.
你应该尝试这个:
这适用于最新的 Chrome。我们遇到了同样的问题,值为
onbeforeunload
的e.returnValue
解决了我的问题。你的代码应该是这样的:
You should try this:
This works on latest Chrome. We had the same issue the
e.returnValue
with value ofonbeforeunload
solved my problem.Your code should be like this:
在 chrome 21.0.1180.79 上确认了此行为,
这似乎与 XSS 具有相同的限制,如果您刷新页面或在同一域+端口上打开页面,则会执行脚本,否则只有在返回时才会执行将显示一个字符串(或类似的字符串)和一个对话框,询问用户是否想要倾斜或留在页面中。
这是一件令人难以置信的愚蠢的事情,因为 onunload/onbeforeunload 不仅用于询问/阻止页面更改。
就我而言,我也使用它保存在页面编辑期间完成的一些更改,并且我不想阻止用户更改页面(至少 chrome 应该尊重返回 true 或更改页面,而不询问返回是否不是字符串),脚本运行时间限制就足够了。
这在 Chrome 中特别烦人,因为卸载页面时 onblur 事件不会发送到编辑元素,Chrome 只是忽略当前页面并跳转到另一个页面。因此,保存更改的唯一更改是卸载过程,如果用户想要更改它,如果没有愚蠢的问题,现在就无法完成...当然他想要,而我不想阻止这一点...
希望 chrome很快就会以更优雅的方式解决这个问题。
Confirmed this behavior on chrome 21.0.1180.79
this seems to work with the same restritions as XSS, if you are refreshing the page or open a page on same domain+port the the script is executed, otherwise it will only be executed if you are returning a string (or similar) and a dialog will be shown asking the user if he wants to leans or stay in the page.
this is an incredible stupid thing to do, because onunload/onbeforeunload are not only used to ask/prevent page changes.
In my case i was using it too save some changes done during page edition and i dont want to prevent the user from changing the page (at least chrome should respect a returning true or change the page without the asking if the return is not a string), script running time restrictions would be enought.
This is specially annoying in chrome because onblur event is not sent to editing elements when unloading a page, chrome simply igores the curent page and jumps to another. So the only change of saving the changes was the unload process and it now can't be done without the STUPID question if the user wants to change it... of course he wants and I didnt want to prevent that...
hope chrome resolves this in a more elegant way soon.
试试这个,它对我有用:
Try this, it worked for me:
试试这个。我已经尝试过并且有效。有趣,但成功消息不像其他消息那样需要确认。
Try this. I've tried it and it works. Interesting but the Succes message doesn`t need confirmation like the other message.
以上都不适合我。我正在从内容脚本发送消息 -> before unload 事件函数中的后台脚本。有效的是当我在清单中将持久性设置为 true(事实上,您可以完全删除该行)时:
逻辑在这个SO问题中进行了解释 此处。
None of the above worked for me. I was sending a message from the content script -> background script in the before unload event function. What did work was when I set persistent to true (in fact you can just remove the line altogether) in the manifest:
The logic is explained at this SO question here.
不知道它是否允许自定义消息显示在浏览器上。
Don't know if it allows custom messages to display on the browser.
当前版本的 Chrome 需要设置事件的
returnValue
属性。仅从事件处理程序返回字符串不会触发警报。Current versions of Chrome require setting the event's
returnValue
property. Simply returning a string from the event handler won't trigger the alert.我在 MacOS High Sierra 上运行 Chrome,并有一个 Angular 6 项目,其中我处理 window.beforeunload 和 window.onbeforeunload 事件。你可以这样做,它对我有用:
当我尝试将字符串放入 event.returnValue 时,它显示一个错误,它需要一个布尔值。
I'm running Chrome on MacOS High Sierra and have an Angular 6 project whithin I handle the window.beforeunload an window.onbeforeunload events. You can do that, it's worked for me :
It show me an error when I try to put a string in event.returnValue, it want a boolean.
在 Chrome 113 中仍然可以工作,具体来说应该在关键字“return”之后的代码块的最后一行放置一个函数(我放置 afunc() 而不是典型的模式,自定义消息),该函数内的代码应该在其中执行异步操作为了避免解雇某些异常,随后代码块的其余部分将不会被执行。
Still work in Chrome 113, specifically should put a function at the last line of code block after keyword 'return' (I put afunc() instead of typical pattern, a custom message), where code inside this function should do your asynchronous stuffs in order to avoid a dismissal something exception and subsequently the rest of your code block will not be executed.