简体   繁体   English

JavaScript退出弹出功能在Chrome和IE中无法正常工作

[英]JavaScript exit popup function doesn't work properly in Chrome and IE

I have an exit popup js function which displays an alert and adds something to the url (and redirects) when someone tries to leave the page. 我有一个退出弹出式js函数,它会显示警报,并在有人尝试离开页面时向URL添加一些内容(并重定向)。

The alert is displayed in all browsers but the code: 该警报显示在所有浏览器中,但代码如下:

window.location.href = "?p=exit" 

is not executed in Chrome and IE. 无法在Chrome和IE中执行。

It works fine in Firefox. 它在Firefox中工作正常。 When you reload the page an alert is displayed and the url is modified. 重新加载页面时,将显示警报,并修改URL。

Take a look at the source code, it is very simple. 看一下源代码,这很简单。

Code: 码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
var exit=true;
    function confirmExit()
            {
            if(exit)
            {
            window.location.href = "?p=exit";
            }
            if(exit)
            return "Wait! Don't Leave Empty Handed!\n\nThank you for taking the time to check out our offer! Before you go we have a complimentary crash to help you succeed. Click the 'Cancel' or 'Stay On This Page' button if you're interested!";
            }
</script>
</head>
<body  onbeforeunload="return confirmExit()">
</body>
</html>
  • You cannot (cross-browser) redirect from "onbeforeunload". 您不能(跨浏览器)从“ onbeforeunload”重定向。
  • Chrome blocks alerts set in "onbeforeunload". Chrome会阻止在“ onbeforeunload”中设置的警报。

Check out this answer for more information: https://stackoverflow.com/a/7080331/353710 查看此答案以获取更多信息: https : //stackoverflow.com/a/7080331/353710

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

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