浏览器关闭时显示弹出窗口 [英] Show popup window when the browser close

查看:110
本文介绍了浏览器关闭时显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个电子商务(PHP)网站,这是我的要求.

I'm developing an eCommerce(PHP) web site and here's my requirement.

一旦客户离开订单页面或关闭浏览器, 我想提供另一个带有弹出窗口或警报框的产品. 如果他们选择是",它将重定向到另一个产品页面 而不是关闭窗口.

Once the customer leave the order page or close the browser, I would like to offer another product with pop up or alert box. If they choose 'Yes', it will redirect to another product page instead of closing window.

我在主体"onunload"事件上尝试使用javascript window.open(). 但是浏览器一直在阻止它.

I tried with javascript window.open() on body "onunload" event. But the browsers keep blocking it.

有没有要完成此任务?

Is there anyway to accomplish this?

谢谢你, 丹

推荐答案

首先,这一点对用户都不友好.就像@Dagon所说的那样,没有人希望被限制离开页面,然后显示出垃圾邮件出售.话虽这么说,我相信您有正当的理由,或者被告知要这样做.所以这是我的答案---

First off: This is not user friendly at all. Just like @Dagon said, nobody want's to be restricted from leaving a page and then shown a spam of different items being sold. With that being said, I'm sure you have a legit reason, or rather are being told to do this. So here is my answer---

无法通过onunload事件来执行此操作.触发该事件后,就没有机会重定向或取消卸载,因为它实际上是 卸载事件上.

There is no way to do this with the onunload event. Once that event is being fired, there is no chance to redirect or cancel the unload because it is literally on the unload event.

您最好的机会是在onbeforeunload事件中.这是唯一实际暂停onunload事件并且也可以取消onunload执行的事件. onbeforeunload事件有两个不同的结果

Your best chance to do this will be in the onbeforeunload event. This is the only event that actually pauses the onunload event and can also cancel the onunload from executing. The onbeforeunload event has two different outcomes

  • 如果您在onbeforeunload事件中返回了某些内容,则当用户尝试离开时,将显示一个默认对话框,其中包含您的文本.用户可以单击确定"或取消".如果他们单击确定,浏览器将继续导航.如果用户单击取消",则您猜测onunload事件被取消.令人惊讶的是,这似乎是取消onunload事件而不锚定的唯一方法.
  • 如果您不返回任何内容,则onbeforeunload事件中的代码将在用户不知情的情况下触发. (此代码必须相对较短,因为onbeforeunload事件不会占用很多时间.

您可能想尝试的一个想法(我从未尝试过)是尝试将超链接添加到onbeforeunload事件的return语句中.同样,不知道这是否行得通.

An idea you might want to try (I've never attempted this) is try adding a hyperlink into the return statement of the onbeforeunload event. Again, no idea if this will work.

以下是onbeforeunload和onunload事件的非常简单的示例:

Below is a very simple sample of the onbeforeunload and onunload events:

<script type="text/javascript"> //may not be neccesary for your code

window.onbeforeunload=before;
window.onunload=after;

function before(evt)
{
   return "This will appear in the dialog box allong with some other default text";
   //If the return statement was not here, other code could be executed silently (with no pop-up)
}

function after(evt)
{
   //This event fires too fast for the application to execute before the browser unloads
}

</script>

我知道您想创建警报或确认弹出窗口,但这也有一些问题.典型的程序员无法访问onbeforeunload和onunload事件的源代码,因此没有人可以100%确信他们所做的一切.从我的测试中知道,让自定义弹出窗口只出现一次并执行其他代码似乎是不可能的.

I know you want to create an alert or a confirm pop-up but that has some issues too. The typical programmer does not have access to the source-code of the onbeforeunload and onunload events, so no one is 100% sure of everything they do. What I know, from my testing, is that it seems impossible to have a custom pop-up appear only once and also execute other code.

如果用户关闭网页,则捕获onbeforeunload事件中的唯一方法.那是不可能的.如果用户使用后退"按钮,则还会在该事件上触发onb​​eforeunload事件.我知道您的最终目标是什么,但是,如果可以的话,我有个建议.尝试在页面上锚定链接或按钮.如果绝对需要此弹出窗口,则唯一可行的方法是将弹出窗口锚定到网页上的链接/按钮.但是,当然,只有当他们尝试使用您的链接导航(这会更加方便用户使用)时,此方法才有效;但是,如果他们尝试使用外部链接(如收藏夹链接或关闭浏览器)进行导航,它将可以不被执行.

If the user is closing the webpage the only way to capture that is in the onbeforeunload event. There's no way out of that one. If the user uses the back button, the onbeforeunload event is also fired there. I know what your end goal is, but I have a suggestion, if allowable of course. Try anchoring links or buttons on your page. If it is absolutely neccesary to have this pop-up, the only sure-fire way of doing this would be to anchor the pop-up to links/buttons that are on your webpage. But of course this would only work if they are trying to navigate away using your links (which would be a little more user-friendly) but if they try to navigate away using external links (like favorite links or closing the browser) then it would not be executed.

祝您好运. onbeforeunload和onunload非常棘手.

Best of luck in your endeavours. Onbeforeunload and onunload are tricky.

这篇关于浏览器关闭时显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆