How to get window.onbeforeunload to work for different browsers

Posted on 2011-10-04
Last Modified: 2012-05-12
I have written some code to redirect to another page, in an 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") }

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" ""> for <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> to no avail. Any Suggestions?
Question by:Jhibumi
    LVL 81

    Expert Comment

    no way, each one have its policy to protect users

    Author Comment

    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?
    LVL 81

    Accepted Solution

    the code itself is fine, there's nothing you can do to improve it. each browser have its own policy, for example most of them allow you to display an alert when you go to an other page (and not closing the browser)

    window.onbeforeunload = function() {
           return "bye!";
    LVL 81

    Expert Comment


    Author Closing Comment

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now