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

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

本文关键字:Jquery 显示 弹出式 调查 离开      更新时间:2023-09-26

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

所以我想离开页面只有当他回答了调查的所有问题和/或关闭弹出窗口…我该怎么做呢?

我试试这个代码

$(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>

open survey content in modal for interaction…按照这些链接在你的页面上创建模态弹出窗口。

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?";
  }
}