Jquery colorbox to open on window close
Hi I am开发者_如何学Go stuck at this point. How can I open a colorbox on window close.
I can open a colorbox on click of any div class.
I want a colorbox to pop out when user hits the close button of the browser or window.
Any help here would be highly appreciated.
Thanks in Advance
Generally you can't stop user from leaving the page by showing him colorbox (or anything else for that matter).
However, there are two events that you can listen to onbeforeunload and onunload. By settings returnValue property on event object in handler of beforeunload event you tell the browser to popup a question window that asks if user wants to stay on this site or leave. If user decides to stay then onunload event isn't fired if he decides to leave however, there's no way to cancel the action, you can only postpone the actual leave. You can postpone leaving by using alert() or some some loop that runs for a specified time. Check examples below.
// 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;
Do you mean window.onbeforeunload?
window.onbeforeunload = function () {
// do something here
};
精彩评论