javascript,如何使用 onbeforeunload 查找按下的按钮

发布于 2024-12-04 14:53:02 字数 455 浏览 1 评论 0原文

我正在做这样的弹出警报:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-12-11 14:53:02

正如 pimvdb 在对你的问题的评论中所说,这是一件非常烦人的事情,我强烈建议不要这样做。这只会激怒您的潜在客户,并确保他们以后不想再回到您的网站。

也就是说,您可以尝试以下方法。我不确定它会起作用,我可以看到有一些保护措施可以防止你在窗户卸载时乱搞东西,但我会做类似的事情:

window.onbeforeunload = confirmExit;
var triedToExit = false;
function confirmExit()
{
  triedToExit = true;
  return "Wait! Save up to $20 Today! \n nClick OK to Save";
}

从那里你经常进行一些功能检查查看是否设置了 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:

window.onbeforeunload = confirmExit;
var triedToExit = false;
function confirmExit()
{
  triedToExit = true;
  return "Wait! Save up to $20 Today! \n nClick OK to Save";
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文