Link to home
Start Free TrialLog in
Avatar of Jhibumi
Jhibumi

asked on

How to get window.onbeforeunload to work for different browsers

I have written some code to redirect to another page, in an asp.net 2.0 application, when the browser is closed:

<script type="text/javascript">
     
      //if application terminated by closing browser then redirect to another page to remove entry for "one session only per account"
      window.onbeforeunload = function() {
      //if (window.event.clientY < -80) { $(location).attr('href', "AbnormalCloseDown.aspx") }
          alert(window.event.clientY)
      };
 
  </script>

However, it does not work for browsers other than IE (using version 8). I need to get it working, if possible for Firefox 6/7 and Chrome 14 and would prefer also to get it working for Safari 5. I've tried changing the browser mode substituting <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd"> for <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> to no avail. Any Suggestions?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

no way, each one have its policy to protect users
Avatar of Jhibumi
Jhibumi

ASKER

The code I gave should not have the line if (window.event.clientY < -80) { $(location).attr('href', "AbnormalCloseDown.aspx") } commented out and the line alert(window.event.clientY) removed.

If it is not possible to use the same code for other browsers then is there some other way to implement it for firefox and chrome?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jhibumi

ASKER

My ultimate problem was how to only allow one session at any one time per user account. The chosen approach being to leave the first login and prevent any further attempts while it is active. To manage the case where the user terminates the application by closing the browser I needed to get rid of an application 'token'. To do this I found a suggestion to do this by redirecting to another page on unload. This works for IE using <body onunload="F1()"> where FI is a function to redirect if window.event.clientY < -80. Doing this for IE using window.onbeforeunload also works. I found that to get it working for other browsers rather than redirecting in window.onbeforeunload you need to use AJAX so that the code in Page_Load is simply executed.