如何在离开页面时用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..您不能直接使用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>
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?";
}
}
相关文章:
- 重写CSS:使用jquery显示none属性
- 使用mvc和jquery显示更多点击数据
- 如何使用jquery显示具体的li数
- PHP/JQUERY-显示json的每个子数组
- 如何使用jQuery显示JSON数据
- 使用jquery显示特定的li标记
- JavaScript/jQuery:显示DIV,直到用户停止在Textbox中键入
- Jquery显示的导航不'单击'主页'
- 如何避免试图用php+jquery显示php起始页的无限循环
- 使用Jquery显示隐藏表单字段
- 使用jquery显示mysql查询结果
- 最有效的jquery显示/隐藏切换
- 如何使用jQuery显示/隐藏菜单
- jQuery 显示未按预期执行
- 如何使用 jquery 显示所选选项值
- 使用 JQuery 显示来自 URL 的图像
- 如果选择了多个选项,则使用 jQuery 显示隐藏的表单元素
- 如何使用 JQuery 显示带有加载符号的数据表
- jquery 显示/隐藏切换在 Internet Explorer 中损坏
- 使用 jQuery 显示和隐藏菜单