javascript,如何使用 onbeforeunload 查找按下的按钮
我正在做这样的弹出警报:
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Wait! Save up to $20 Today! \n nClick OK to Save";
}
当我关闭窗口时,我会 留在页面上
和 关闭浏览器
或类似的事情
我想做的是:如果选择了“留在页面上”选项,然后我想将用户重定向到另一个页面或显示 jquery 弹出窗口。
类似于:
if (window.onbeforeunload = null){
location.assign('http://example.com');
}
但这不起作用。
有什么想法吗?
谢谢
i am doing a popup alert like this:
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Wait! Save up to $20 Today! \n nClick OK to Save";
}
when i close the window i vet Stay on the page
and Close the browser
or something similar
what i am trying to do is: if Stay on the page
option has been chosen then i want to eighter redirect the user to another page or show a jquery popup.
something similar to:
if (window.onbeforeunload = null){
location.assign('http://example.com');
}
but this doesn't work.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 pimvdb 在对你的问题的评论中所说,这是一件非常烦人的事情,我强烈建议不要这样做。这只会激怒您的潜在客户,并确保他们以后不想再回到您的网站。
也就是说,您可以尝试以下方法。我不确定它会起作用,我可以看到有一些保护措施可以防止你在窗户卸载时乱搞东西,但我会做类似的事情:
从那里你经常进行一些功能检查查看是否设置了 attemptsToExit,如果设置了,则可以正常转发它们。
但实际上,不要这样做。
As pimvdb said in his comment to your question, this is a really annoying thing to do and I highly recommend against it. It's just going to piss off your potential customers and ensure they don't want to come back to your site later.
That said, here's something you can try. I'm not positive it'll work, I could see there being some safeguards in place to prevent you from mucking with things while the window's unloading, but I'd do something like:
From there you have some function checking every so often to see if triedToExit is set, and if it is, you can forward them as normal.
But really, don't do this.