简体   繁体   English

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

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

I want to show survey in pop-up window when users leaves page. 我想在用户离开页面时在弹出窗口中显示调查。

So I want to leave page only when he answers all the questions of survey and/or close the pop-up window... How can I do that? 因此,我只想在他回答所有调查问题和/或关闭弹出窗口时才离开页面。我该怎么做?

I try this code 我试试这段代码

$(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();
        });
    });

But it blocks by majority of browsers and don't pause clicked action(going to other page or closing the window). 但是它被大多数浏览器阻止,并且不暂停单击的操作(转到其他页面或关闭窗口)。 What is the best way to do that? 最好的方法是什么?

Try Using window.onbeforeunload event .. 尝试使用window.onbeforeunload事件..

have an idea from the follow code snippet. 从以下代码片段中获得一个想法。 i am using this type of code to confirm use to before leaving the page.. means either it is close tab or refresh the page... 我在离开页面之前使用这种类型的代码确认使用..意味着它是关闭选项卡或刷新页面...

But you have to track when it is refresh or close.. both means same to beforeunload.. you can't use directly use them either unload or beforeunload - they do not differ between window close, Try this may be it will work according to your requirements. 但是您必须跟踪刷新或关闭的时间..两者都与beforeunload相同..您不能直接使用它们进行unloadbeforeunload它们在关闭窗口之间没有区别,请尝试这样做,因为它可能会根据您的要求。

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

jQuery: jQuery的:

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

or javascript:

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

For more information follow this: https://developer.mozilla.org/en/DOM/window.onclose 有关更多信息,请访问: 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>

open survey content in modal for interaction... follow these links to create modal popup on your page .. 以模态打开调查内容以进行交互...单击这些链接可以在页面上创建模态弹出窗口。

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

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

Is it necessary to make users fill out a survey? 有必要让用户填写调查问卷吗? If not, then you could replace the contents of the body so the user sees it in the background. 如果不是,则可以替换正文的内容,以便用户在后台看到它。

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