0

我正在尝试从某些页面启动弹出窗口。我希望用户仅在以下情况下看到弹出窗口:

a) 他们关闭浏览器选项卡 b) 他们点击浏览器的后退按钮 c) 他们转到购买路径中下一步以外的页面 - 基本上,当他们转到任何不包含字符串“/checkout/”的 URL 时”。

我已经查看了加载正文 onUnload 的弹出示例,这有点过于全球化了。我只想要一个弹出窗口——一个真正的实际弹出窗口——如果他们试图离开结帐路径就启动。

我在 StackOverflow 上查看了几个问题,但似乎没有一个能完全回答我的问题。此答案https://stackoverflow.com/a/3753963/1634459中提到的 onBeforeUnload可能有效......可能......但我不完全确定如何在上面列出的条件下实现它。

...帮助?

这是我想使用的弹出窗口代码:

<script language="javascript" type="text/javascript">
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="center"){LeftPosition=(screen.availWidth)?(screen.availWidth-w)/2:50;TopPosition=(screen.availHeight)?(screen.availHeight-h)/2:50;}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);
if(win.focus){win.focus();}}
function CloseNewWin(){if(win!=null && win.open)win.close()}
window.onfocus=CloseNewWin;
</script>
4

1 回答 1

0

干得好; 我在之前的工作中为营销团队写了这篇文章。你应该能够很容易地适应它;只需根据需要更新 URL 和参数。

当用户离开页面时,此代码将打开一个弹出窗口,除非他们通过提交结帐表单离开。(如果您在页面上有多个表单,而您不应该在结帐流程中使用,那么您也需要修复它。)

var nag = function () {

    return {
        // assume the user is going to leave, get ready to pounce!
        doWant: true,

        // FALCON PUNCH
        deploy: function () {
            if (nag.doWant) {

                var width = 449;
                var height = 298;

                args = "width=" + width + ",";
                args += "height=" + height + ",";
                args += "location=0" + ","
                args += "menubar=0" + ","
                args += "resizable=1" + ","
                args += "scrollbars=0" + ","
                args += "status=0" + ","
                args += "titlebar=0" + ","
                args += "toolbar=0" + ","
                args += "hotkeys=0"

                var demo_window = window.open("YOUR_URL", "main", args);

                try {
                    demo_window.focus();
                } catch (err3) { };
            }
        },

        // oh, off to step 2? that's cool. carry on.
        ignore: function () {
            nag.doWant = false;
        }
    }
} ();

window.onunload = nag.deploy;
document.forms[0].onsubmit = nag.ignore;

If you want to be able to STOP the user from leaving the page, I can help you with that update.

于 2012-09-21T20:42:05.650 回答