简体   繁体   English

禁用刷新页面上的javascript弹出窗口

[英]disable javascript popup on refreshing page

I have written code like this to throw a pop up when my website is closed. 我已经写了这样的代码,当我的网站关闭时弹出一个弹出窗口。 But this throws the pop up even if i refresh my page. 但这会弹出弹出窗口,即使我刷新页面也是如此。 Please help me to throw popup only when i close the browser of that website. 请仅在我关闭该网站的浏览器时帮助我抛出弹出窗口。 (disable popup on refreshing page) (禁用刷新页面上的弹出窗口)

<body onunload="javascript: exitpop()">

<script type="text/javascript">
function exitpop()
{
my_window= window.open ("","mywindow1","status=1,width=600,height=400"); 
my_window.document.write('<h1>My Team</h1><p>Thank you</p><p>If you accidentally closed website click <a href="http://www.google.com">here</a> to go back to our website</p>'); 
}.

onUnLoad is called on a refresh because the browser is requesting a new page (well, the same page in the case of a refresh, but it is still a new call). 刷新时会调用onUnLoad,因为浏览器正在请求一个新页面(刷新时是同一页面,但仍然是一个新调用)。

I am not sure if there is a way to check where the user is going next, but if there is you could compare it to their current url, if they are the same then the user is just refreshing and the script doesn't need to be run. 我不确定是否有办法检查用户的下一步操作,但是如果可以将其与他们当前的网址进行比较,如果它们相同,则用户只是在刷新,脚本不需要运行。

Good luck 祝好运

Sadly, this is not possible. 可悲的是,这是不可能的。 To the browser, it is essentially the same action. 对于浏览器,它本质上是相同的动作。

However, if you just use a confirm() box, you can catch a user that is trying to leave without linking them back. 但是,如果只使用confirm()框,则可以捕获正在尝试离开的用户而无需将他们链接回去。 If they are refreshing or if they really want to leave, hitting "OK" will allow that action to occur. 如果他们正在刷新或真的想离开,则单击“确定”将允许该操作发生。

Let me know if you need this explained further. 让我知道是否需要进一步解释。

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

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