嗨,我被困在这一点上。如何在窗口关闭时打开彩色框。
我可以点击任何div类打开一个颜色框。
当用户点击浏览器或窗口的关闭按钮时,我希望弹出一个颜色框。
这里的任何帮助都将受到高度赞赏。
先谢谢
答案 0 :(得分:0)
您的意思是window.onbeforeunload吗?
window.onbeforeunload = function () {
// do something here
};
答案 1 :(得分:0)
一般情况下,您无法通过向他显示颜色框(或其他任何内容)来阻止用户离开页面。
但是,有两个事件可以监听 onbeforeunload 和 onunload 。 通过在beforeunload事件处理程序中的事件对象上设置returnValue属性,您可以告诉浏览器弹出一个问题窗口,询问用户是想留在此站点还是离开。如果用户决定留下,那么如果他决定离开,则不会触发onunload事件,但是无法取消该操作,您只能推迟实际休假。您可以使用alert()或某些运行指定时间的循环来推迟离开。请查看以下示例。
// showLeaveWarning is some imaginary variable that determines wherever you want to show the warning about leaving the page
beforeUnloadHandler = function(e) {
if (showLeaveWarning) {
var e = e || window.event, msg = "Don't leave, please!"
if (e) {
e.returnValue = msg; // Chrome won't respect the msg you're trying to show but Firefox will
}
return msg;
}
}
unloadHandler = function(e) {
// you can do something here - like sending an ajax request to your application to know that user X has left the page
var a = (new Date()).getTime() + 1500; // 1500 is a number of ms that loop will run for
// the loop below is necessary to try to force the browser to wait couple more seconds while (for example) the ajax request is finished
while ((new Date()).getTime() < a) {} // do this loop for 1500ms (Firefox v6 seems to wait for as long as you please (by changin 1500 to something else in line above)
}
}
window.onunload = unloadHandler;
window.onbeforeunload = beforeUnloadHandler;