JavaScript On Close of window in IE8

前端 未结 3 1540
忘了有多久
忘了有多久 2020-12-20 02:07

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

window.onbeforeunload = function (evt) {

    evt = (evt)? evt:event;
    clickY = evt.clie         


        
相关标签:
3条回答
  • 2020-12-20 02:45

    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.

    0 讨论(0)
  • 2020-12-20 02:59

    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.

    0 讨论(0)
  • 2020-12-20 03:05

    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

    0 讨论(0)
提交回复
热议问题