Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...