javascript - Java Script On Close of window in IE8 - Stack Overflow

admin2025-04-20  1

I have implemented some function when the browser will be closed.

window.onbeforeunload = function (evt) {

    evt = (evt)? evt:event;
    clickY = evt.clientY;
    altKey = evt.altKey; 
    keyCode = evt.keyCode; 

if (altKey == true && keyCode == 0){ 
            //Make call to the server to clear the cache                   
    }else if(clickY < 0){
     //Make a call to the server to clear the cache
    }else {
        return;
    }             
};

Now the problem in the above code is when ever i press enter by clicking in the addressbar or i refreshed the page with refresh button my code gives me alert("21112") which i don't want because the call will trigger only and only if the browser is closed. So can somebody help me to achieve this.

I have implemented some function when the browser will be closed.

window.onbeforeunload = function (evt) {

    evt = (evt)? evt:event;
    clickY = evt.clientY;
    altKey = evt.altKey; 
    keyCode = evt.keyCode; 

if (altKey == true && keyCode == 0){ 
            //Make call to the server to clear the cache                   
    }else if(clickY < 0){
     //Make a call to the server to clear the cache
    }else {
        return;
    }             
};

Now the problem in the above code is when ever i press enter by clicking in the addressbar or i refreshed the page with refresh button my code gives me alert("21112") which i don't want because the call will trigger only and only if the browser is closed. So can somebody help me to achieve this.

Share Improve this question edited May 17, 2012 at 14:28 BOSS asked May 15, 2012 at 9:42 BOSSBOSS 2,9518 gold badges30 silver badges53 bronze badges 5
  • Then check for enter key and for F5 key... – gdoron Commented May 15, 2012 at 9:44
  • 2 You cannot do this - the event is fired when the current page is navigated away from - this means navigated to a different page or browser closed – Manse Commented May 15, 2012 at 9:44
  • @gdoron it always give keycode 0 – BOSS Commented May 15, 2012 at 9:44
  • Isn't this one of the underhand ways to make people stay on the site? – Undefined Commented May 17, 2012 at 12:34
  • 2 I've tried this myself once and, of course, it didn't work. The reason is logical, if you think about it: if you were allowed to do this, the user would have to close the browser from the Process Manager. – d4rkpr1nc3 Commented May 17, 2012 at 13:47
Add a ment  | 

3 Answers 3

Reset to default 8 +50

window.onbeforeunload event is calls before your page is unloaded. There is no method to check if it's refresh or browser window close event.

It fires by different scenarios MSDN:

  • Close the current window.
  • Navigate to another location by entering a new address or selecting a Favorite.
  • Click an anchor that refers to another document.
  • Invoke the anchor.click method.
  • Invoke the document.write method.
  • Invoke the document.close method.
  • Invoke the window.close method.
  • Invoke the window.navigate or NavigateAndFind method.
  • Invoke the location.replace method.
  • Invoke the location.reload method.
  • Specify a new value for the location.href property.
  • Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
  • Invoke the window.open method, providing the possible value _self for the window name.
  • Invoke the document.open method.
  • Click the Back, Forward, Refresh, or Home button.

And you can't handle this.

Also note that window.onbeforeunload event supposed to inform user that he leaves page and handler function should return string value that will be included in confirmation popup (except Firefox 4+ see bug).

You can not force the user to stay on the page.

Note: The only thing you can do with onbeforeunload is to notify the user that he is leaving your page (this is because some evil pages can open another pages and so on... and spam users. This is security restriction).

If you want to make AJAX call before window is unloaded you need to use onunload event. Note that your AJAX call must me synchronus. Asynchronous call will not work.

The guy in this thread says that his approach based on the onbeforeunload event works in IE.

IMHO, however, your best (and cross-platform) chance to do what you want is to setup a Ajax polling request (here is a nice demo) to check how much time has passed since the last user activity and clear the cache accordingly.

I worked on this same problem for awhile so I will explain to you what I found out.

1) the window.onbeforeunload event is handled by each browser a little differently - i've seen the session get lost as soon as the event fires regardless if the user chooses to stay on the page or not.

2) a web application simply doesn't have the power to prevent a user from closing the browser or navigating away from a page (for obvious reasons) but by implementing a server side hook I was able to call the necessary clean up code only when the browser was closed so I didn't bother prompting the user (which is kind of like hijacking their browser anyway)

If you need to prompt the user before closing the window then you are going to prompt them when they type in a new url as well. They are one and the same

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149746a287547.html

最新回复(0)