简体   繁体   English

window.onbeforeunload错误

[英]window.onbeforeunload error

We currently have a JS that popups a window whenever the visitor wants to leave the site. 当前,我们有一个JS,可在访问者要离开站点时弹出一个窗口。 It works fine, except when hitting the back button on the browser, it popups up the alert even if we are on the site. 它工作正常,除了在浏览器中单击后退按钮时,即使我们在站点上,它也会弹出警报。 we want to fix this. 我们要解决此问题。

Please note: -we do not want to disable the back button -we do not want to erase the historial 请注意:-我们不想禁用后退按钮-我们不想删除历史记录

the JS function is onbeforeunload. JS函数是onbeforeunload。 it is currently loading everytime the user wants to leave the site. 当前,每次用户要离开该网站时,它都会加载。 When the user navigates the site, on every link that we DO NOT want to trigger the popup, we add this: onClick="changes=true;" 当用户浏览网站时,在我们不想触发弹出窗口的每个链接上,我们添加以下内容:onClick =“ changes = true;” to the href. 到href。

As I mentioned, problem is when user hits back button, FWD and sometimes refresh. 正如我提到的,问题在于用户按下后退按钮,FWD并有时刷新。

here is the website url link text 这是网站网址链接文字

Remove the onbeforeunload-Event entirely. 完全删除onbeforeunload-Event。 Don't bully your visitors! 不要欺负您的访客! If they want to leave your website, you shouldn't try to keep them there, it's annoying. 如果他们想离开您的网站,则不应尝试将其保留在那里,这很烦人。

The only valid reason to use the onbeforeunload-event is when the user has changed something on the site and tries to navigate away from the site without saving the change. 使用onbeforeunload-event的唯一有效理由是当用户更改了站点上的某些内容并尝试在不保存更改的情况下离开站点导航。

+1 what stefanw said. +1 Stefanw说的话。 Don't do this. 不要这样 Everyone will point and laugh at your site. 每个人都会嘲笑您的网站。

You can't tell the difference in an unload between someone closing your window and someone using the back/forwards/refresh buttons, or bookmarks, or just typing a new address. 您无法分辨关闭窗口的人与使用后退/前进/刷新按钮,书签或只是输入新地址的人之间的卸载差异。

The only way to do it would be to have another (eg. pop-up) window present that keeps monitoring the other window to check the URL. 唯一的方法是提供另一个(例如,弹出式)窗口,该窗口不断监视另一个窗口以检查URL。 If window.closed , or you can't access the URL due to same-origin restrictions, the user has left your site and then you can pop something up nagging at them. 如果window.closed ,或者由于同源限制而无法访问URL,则该用户已离开您的站点,然后您可以弹出一些内容来烦扰他们。

Especially don't do this. 尤其不要这样做。 Everyone hates pop-ups and would just close them. 每个人都讨厌弹出窗口,只会关闭它们。 Everyone hates nags. 每个人都讨厌na。 Pop-ups and nags together are a recipe for no more site visitors. 弹出窗口和小控件共同构成了不再需要站点访问者的诀窍。

There again, I've just looked at what your site is. 再次,我只是看了一下您的网站。 Go ahead with the pop-ups. 继续弹出窗口。 Or why not just delete your site and find something actually useful to do, instead of affiliating yourself with the worst spammers on the net? 还是为什么不删除站点并找到实际有用的方法,而不是使自己成为网络上最差的垃圾邮件发送者呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM