Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var focused = true;
- // Set the name of the hidden property and the change event for visibility
- var hidden, visibilityChange;
- if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
- hidden = "hidden";
- visibilityChange = "visibilitychange";
- } else if (typeof document.mozHidden !== "undefined") {
- hidden = "mozHidden";
- visibilityChange = "mozvisibilitychange";
- } else if (typeof document.msHidden !== "undefined") {
- hidden = "msHidden";
- visibilityChange = "msvisibilitychange";
- } else if (typeof document.webkitHidden !== "undefined") {
- hidden = "webkitHidden";
- visibilityChange = "webkitvisibilitychange";
- }
- function handleVisibilityChange() {
- if (document[hidden]) {
- focused = false;
- } else {
- focused = true;
- }
- }
- if (typeof document.addEventListener === "undefined" ||
- typeof hidden === "undefined") {
- alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
- } else {
- // Handle page visibility change
- document.addEventListener(visibilityChange, handleVisibilityChange, false);
- }
- var leave_message = "Please be aware, you are about to be redirected to another location!";
- function goodbye(e)
- {
- if(!window.top.document.hasFocus() && !focused)
- {
- if(!e) { e = window.event; }
- //e.cancelBubble is supported by IE - this will kill the bubbling process.
- e.cancelBubble = true;
- e.returnValue = leave_message;
- //e.stopPropagation works in Firefox.
- if (e.stopPropagation)
- {
- e.stopPropagation();
- e.preventDefault();
- }
- //return works for Chrome and Safari
- return leave_message;
- }
- }
- window.onbeforeunload=goodbye;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement