1

The scenario I have is a bit complicated, but I can reproduce my problem shortly in the following example:

<html>
 <head>
  <script type="text/javascript">
  function fnUnloadHandler() {
       alert("Unload event.. session termination request to be sent to server");
  }
  function f() { 
       i = 1;
       while(1==1){
          i++;
       }
   }
  </script>
 </head>
<body onbeforeunload="fnUnloadHandler()">
 <p><button href="#" onClick="f();" >do</button></p>
 </body>
</html>

Now; once the "do" button is clicked, f() get's the browser in an infinite loop. during that time if I close the page I get two different behaviours :

  • On IE7: Javascript execution is terminated, the onbeforeunload event is triggered and alert is shown then page is closed.
  • On IE9: Javascript execution is terminated, the onbeforeunload event is NOT triggered and NO alert is shown and page closed directly.

I would like to know why IE9 is not handling the onbeforeunload correctly in this case .. is there any fix/patch to apply to IE9 to fix this problem?

Many thanks in advance!

Firas

416E64726577
  • 2,214
  • 2
  • 23
  • 47
Firas
  • 277
  • 4
  • 11
  • I would guess that's beacuse in IE7 there are 2 different threads for handeling the f() call and the onbeforeload event... But what exactly are you trying to achive? Why you want to go into an infinite loop – Shai Aharoni Nov 25 '13 at 09:41
  • Hi Shai, actually I on't have this loop in my product for sure :-), I am just trying to make an abstraction of the problem.. the loop simulates heavy JS processing on IE, which takes up to 5sec. and the problem happens when the page is closed during this period. – Firas Nov 25 '13 at 10:07

2 Answers2

2

JavaScript is, at least most, if not all, of its implementations are single-threaded. If you call the f function, which contains an infinite loop, that one JS thread is busy. The onbeforeunload handler is queued, and it will be called when the function f returns. Unfortunately, f will never return. Some implementations, though, pick up on constructs like while(1==1), and will terminate that loop, because it's not going anywhere. IE often presents you with a conform alert, saying something along the lines of "A script is running, and doesn't look like it's getting anywhere, do you wish to terminate it?"

The only reason, logically, why the onbeforeunload handler will get called is that if either the f function is not called, or if the implementation of a specific browser won't just ditch the call queue when an error is encountered, and the handler will be called, still.
As an asside, some code-review:

function f()
{
    i = 1;
}

is creating an evil implied global variable, change it to:

function f()
{
    var i = 1;
}

to avoid cluttering the global namespace. Also, the onbeforeunload event is generally attached to the global object, lest you want to handle the unload of a specific element. If you want to handle a global unload (client leaving the page), IE7 and IE8 will leak memory, check the solution here, that code example shows how you can avoid leaking memory with event handlers attached to the global object.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

Please see this link http://msdn.microsoft.com/en-us/library/ie/ms536907%28v=vs.85%29.aspx I hope this will help you

<!DOCTYPE html>
<html>
<head>
<script>
  function closeIt()
  {
    return "any string";
  }
  window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to 
      www.microsoft.com</a>
</body>
</html>
jai lalawat
  • 73
  • 1
  • 11
  • `window.onbeforeunload` causes mem-leaks, mind you. I've added a link to my answer with the explanation of _how_ that leak may come about, and how to avoid it – Elias Van Ootegem Nov 25 '13 at 09:50
  • You're welcome... we're all here to help, and share what we know. If you don't mind my rep-whoring: an upvote (either for the linked answer, or my answer here) would be a better way of thanking me, in fact: in the help section of this site [you are asked _not_ to add thank you comments](http://stackoverflow.com/help/someone-answers), but vote instead :) – Elias Van Ootegem Nov 25 '13 at 09:56
  • But you are not answered any question only find your own questions answer. – jai lalawat Nov 25 '13 at 09:59
  • Well, that's why I didn't mark my answer as the accepted answer, but I did put it there, simply because I figured that the information I gained might benefit others, and the tooltip (on hovering over the vote-up arrow) says: _"This answer is useful"_, not _"This answer actually answers the question"_. Besides: I answered this question, didn't I? – Elias Van Ootegem Nov 25 '13 at 10:03
  • Sorry There is some mistake. – jai lalawat Nov 25 '13 at 10:08
  • You have nothing to apologize for, you're a new user, I'm just doing my best explaining how to get the most out of this site, for you benefit and mine. The more people use this site as intended, the better it gets for all of us. So: Happy Coding :) – Elias Van Ootegem Nov 25 '13 at 10:12