简体   繁体   English

关闭页面时无法显示窗口

[英]Having trouble displaying a window upon page close

So I'm looking to display a window when the user tries to leave a page, but I'm having quite a bit of trouble. 因此,我希望在用户尝试离开页面时显示一个窗口,但是我遇到了很多麻烦。

<script type="text/javascript">
window.onunload = openWin()
</script>

When I put the above into the Head of the html file the function fires, but upon the page LOADING, not when it closes. 当我将以上内容放入html文件的开头时,该函数将触发,但是会在页面LOADING上触发,而不是在页面关闭时触发。 This happens in IE8, Opera 12 and Chrome 18. (The latter two detect it as a pop-up and block it accordingly) 在IE8,Opera 12和Chrome 18中会发生这种情况。(后两个将其检测为弹出窗口并相应地对其进行阻止)

It's not for any nefarious purpose mind you, I'm just trying to display a summary of orders placed throughout the day; 您并不是出于恶意目的,我只是想显示一整天的订单摘要; The above function is available at any time via a button, but I also need it to display when the page is closed. 可以通过按钮随时使用上述功能,但是关闭页面时我也需要显示该功能。

You are calling the function at the time of assignment. 您正在分配时调用该函数。 What you want to do is assign the function itself to window.onunload . 您要做的就是将函数本身分配给window.onunload Try: 尝试:

window.onunload = openWin;

without the () parentheses. 没有()括号。

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

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