Advertisement
Guest User

Untitled

a guest
Feb 25th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var focused = true;
  2.  
  3. // Set the name of the hidden property and the change event for visibility
  4. var hidden, visibilityChange;
  5. if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
  6.   hidden = "hidden";
  7.   visibilityChange = "visibilitychange";
  8. } else if (typeof document.mozHidden !== "undefined") {
  9.   hidden = "mozHidden";
  10.   visibilityChange = "mozvisibilitychange";
  11. } else if (typeof document.msHidden !== "undefined") {
  12.   hidden = "msHidden";
  13.   visibilityChange = "msvisibilitychange";
  14. } else if (typeof document.webkitHidden !== "undefined") {
  15.   hidden = "webkitHidden";
  16.   visibilityChange = "webkitvisibilitychange";
  17. }
  18.  
  19. function handleVisibilityChange() {
  20.     if (document[hidden]) {
  21.         focused = false;
  22.     } else {
  23.         focused = true;
  24.     }
  25. }
  26.  
  27. if (typeof document.addEventListener === "undefined" ||
  28.     typeof hidden === "undefined") {
  29.     alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
  30. } else {
  31.     // Handle page visibility change  
  32.     document.addEventListener(visibilityChange, handleVisibilityChange, false);
  33. }
  34.  
  35.  
  36. var leave_message = "Please be aware, you are about to be redirected to another location!";
  37. function goodbye(e)
  38. {
  39.     if(!window.top.document.hasFocus() && !focused)
  40.     {
  41.         if(!e) { e = window.event; }
  42.         //e.cancelBubble is supported by IE - this will kill the bubbling process.
  43.         e.cancelBubble = true;
  44.         e.returnValue = leave_message;
  45.         //e.stopPropagation works in Firefox.
  46.         if (e.stopPropagation)
  47.         {
  48.             e.stopPropagation();
  49.             e.preventDefault();
  50.         }
  51.        
  52.         //return works for Chrome and Safari
  53.         return leave_message;
  54.     }
  55. }
  56.  
  57. window.onbeforeunload=goodbye;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement