1

The below code works fine in IE8.

Here is my code:

window.onbeforeunload = function () {

    return  "Are you sure want to LOGOUT the session ?";
}; 

window.onunload = function () {

    parent.logout();
};

But in IE9+

window.onbeforeunload works and I am getting the alert.

But the window.onunload is not working.

I also tried the Group Policies in Internet Explorer 9 to enable the Allow Internet Explorer 8 Shutdown Behavior.For reference , click here

How to make the window.onunload to work in IE9+ ?

Hope our stack users will help me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Human Being
  • 8,269
  • 28
  • 93
  • 136

3 Answers3

0

Not sure why it is not working for you, because I'm pretty sure it's supported in all those IE browsers.

Try this approach, it's probably better overall :) https://developer.mozilla.org/en-US/docs/Web/Reference/Events/unload?redirectlocale=en-US&redirectslug=DOM%2FMozilla_event_reference%2Funload

alexK85
  • 93
  • 6
0

I'm going to hazard a guess that your parent.logout() method performs an asynchronous request to your server. If that's the case, then your mileage is likely to vary between browsers anyway, as most will probably cancel any incomplete requests when the page unloads.

One option is to use a synchronous request, but those are generally bad for usability and cause frustration when your server takes longer than usual to return a response.

See also: Can beforeunload/unload be used to send XmlHttpRequests reliably?

Community
  • 1
  • 1
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • I am sending the synchronous request only. – Human Being Feb 11 '14 at 09:28
  • @HumanBeing: it's possible that synchronous requests aren't allowed either, to prevent an issue where a response takes too long and the browser/tab locks up without navigating. – Andy E Feb 11 '14 at 09:34
-1

Me also same problem not working in IE only

 @HostListener('window:unload', ['$event'])
  unloadHandler($event) {
    this.LogOut();
  }
  @HostListener('window:beforeunload', ['$event'])
  public beforeunloadHandler($event) {
    var msg = this.messageBundle['windowClosemsg']
    $event.returnValue = msg;
    return false
  }