onbeforeunload 似乎在 Safari 或 Chrome 中不起作用

发布于 2024-11-27 05:12:58 字数 830 浏览 1 评论 0原文

我正在此页面上工作: http://www.weloveflyers.co.uk/order.php< /a>

当有人输入订单详细信息并点击“Let's Go!”后订单信息被捕获,显示给他们检查,然后显示“立即付款!”按钮出现在底部(尝试一下,您不必支付任何费用,这样您就可以明白我的意思)

我不想阻止任何人在单击“开始”按钮之前离开页面,但如果他们有已经点击了“我们走吧!” (即下订单)但尚未点击“立即付款!”我希望能够与他们核实“您真的要离开吗?如果您不付款就离开,您的订单将被取消!”

在我进入确保它仅在已下订单时才起作用的复杂问题之前,我尝试了一个非常基本的 onbeforeunload 设置,只是为了看看浏览器是否支持它。我似乎在 Safari 中得到了对该功能的零星支持,通常它不起作用,但偶尔会起作用,而在 Chrome 中它根本不起作用。

任何人可以提供的任何帮助或建议将不胜感激!

谢谢,

菲尔

P.S.供您参考,我暂时从实时页面中删除了所有 onbeforeunload 功能,因为我不想惹恼访问者,直到我得到正确的结果并且它完全按照我想要的方式执行。

更新:这是最新的尝试:

var needToConfirm = true; 
window.onbeforeunload = confirmExit; 
function confirmExit() {
  if (needToConfirm) 
    return "Message";
}; 

I am working on this page: http://www.weloveflyers.co.uk/order.php

After someone has entered their order details and click "Let's Go!" the order information is captured, displayed for them to check and then a "Pay Now!" button appears at the bottom (try it, you don't have to pay anything, just so you can see what I mean)

I don't want to hinder anyone leaving the page BEFORE they click the Let's Go button, but if they have already clicked "Let's Go!" (ie. placed an order) but have NOT yet clicked "Pay Now!" I want to be able to check with them "Do you really want to leave? If you leave without paying, your order will be cancelled!"

Before I get into the complication of making sure it only functions when an order has already been placed, I tried just a very basic onbeforeunload setup just to see if the browser would support it. I seem to get sporadic support for this function in Safari, generally it doesn't work, but very occasionally it does, and in Chrome it just doesn't work at all.

Any help or advice anyone can offer would be gratefully received!

Thanks,

Phil

P.S. For your info, I have removed all onbeforeunload functionality from the live page for now, because I don't want to annoy visitors until I've got it right and it does exactly what I want.

UPDATE: Here is the latest attempt:

var needToConfirm = true; 
window.onbeforeunload = confirmExit; 
function confirmExit() {
  if (needToConfirm) 
    return "Message";
}; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哆兒滾 2024-12-04 05:12:58

根据 MDN 文档,我刚刚在 Chrome 11(和 13)和 Safari 5 中尝试了以下操作OS X 和 Windows 7 上的 Chrome 14 中,它对我来说工作得很好(就像上面的代码一样!):

window.onbeforeunload = function (e) {
  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Safari
  return 'Any string';
};

如果您在页面中完全按照原样使用上面的代码,会发生什么?

您使用什么版本的 Chrome 和 Safari?

As per the MDN documentation, I just tried the following in Chrome 11 (and 13) and Safari 5 on OS X, and in Chrome 14 on Windows 7, and it works fine for me (as does your code above!):

window.onbeforeunload = function (e) {
  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Safari
  return 'Any string';
};

What happens if you use the code above in your page, exactly as it is?

What versions of Chrome and Safari are you using?

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