BOSS
BOSS

Reputation: 2951

Java Script On Close of window in IE8

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.

Upvotes: 2

Views: 2158

Answers (3)

antyrat
antyrat

Reputation: 27765

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.

Upvotes: 8

DaveAlger
DaveAlger

Reputation: 2526

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

Upvotes: 0

Avio
Avio

Reputation: 2708

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.

Upvotes: 0

Related Questions