[英]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相同..您不能直接使用它們進行unload
或beforeunload
它們在關閉窗口之間沒有區別,請嘗試這樣做,因為它可能會根據您的要求。
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.