繁体   English   中英

离开页面时如何使用Jquery显示弹出调查

[英]How to show pop-up survey with Jquery when leaving page

我想在用户离开页面时在弹出窗口中显示调查。

因此,我只想在他回答所有调查问题和/或关闭弹出窗口时才离开页面。我该怎么做?

我试试这段代码

$(document).ready(function () {
        function exitpop() {
            my_window = window.open("http://www.google.com", "mywindow1", "status=off,toolbar=off,location=off,menubar=off,directories=off,resizable=off,scrollbars=off,height=800,width=800");
            });
        }
        $(window).unload(function () {
            exitpop();
        });
    });

但是它被大多数浏览器阻止,并且不暂停单击的操作(转到其他页面或关闭窗口)。 最好的方法是什么?

尝试使用window.onbeforeunload事件..

从以下代码片段中获得一个想法。 我在离开页面之前使用这种类型的代码确认使用..意味着它是关闭选项卡或刷新页面...

但是您必须跟踪刷新或关闭的时间..两者都与beforeunload相同..您不能直接使用它们进行unloadbeforeunload它们在关闭窗口之间没有区别,请尝试这样做,因为它可能会根据您的要求。

http://docs.jquery.com/Events/unload#fn

jQuery的:

$(window).unload( function () { alert("Bye now!"); } );

or javascript:

window.onunload = function(){alert("Bye now!");}

有关更多信息,请访问: https//developer.mozilla.org/en/DOM/window.onclose

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    function confirmBeforeUnload(e) {
        var e = e || window.event;

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

        // For Safari
        return 'Any string';

    }
    function goodbye(e) {
        if (!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    window.onbeforeunload = goodbye;
    </script>
    <title>Untitled Document</title>
    </head>

    <body>
    </body>
    </html>

以模态打开调查内容以进行交互...单击这些链接可以在页面上创建模态弹出窗口。

http://choosedaily.com/1178/15-jquery-popup-modal-dialog-plugins-tutorials/

http://www.queness.com/post/77/simple-jquery-modal-window-tutorial - 按照此步骤教程,使用代码创建模态弹出窗口。

有必要让用户填写调查问卷吗? 如果不是,则可以替换正文的内容,以便用户在后台看到它。

var done = 0;
window.onbeforeunload = function() {
  if (done != 1) {
    done = 1;
    document.getElementsByTagName('body')[0].innerHTML = "new content goes here";
    return "Howdy there. Why don't you stick around for a moment and take our survey?";
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM