2

Say the user closes the tab corresponding to the current page, at moment t1, at which a script was running:

<script>

  foo();

  // t1 <----------

  bar();

</script>

Will the rest of the <script> run? Or is Javascript execution killed immediately?

I can imagine how given a second <script> below the described one, this second script will never run. But maybe the first one is treated like a single, uninterruptible thing?

deprecated
  • 5,142
  • 3
  • 41
  • 62

1 Answers1

4

It's possible this may vary by browser, but probably not. The consensus (see below) seems to be that the script seems to complete, or is at least given several seconds to complete; once it has (or after some period of time, anyway), the browser closes the tab.

You can fairly easily test this on modern browsers by doing a busy-wait and seeing what happens when you try to close the browser during the busy-wait.

For instance, consider this page:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<style>
html, body {
  margin: 0;
  padding: 0;
}
</style>
<body>
<p>I'm here</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var d = Date.now() + 10000;
while (Date.now() < d) {
}
</script>
</body>
</html>
  • Chrome: Shows "I'm here" and if you click the close tab button, it doesn't close for several seconds (seems like the loop completes). (If it never finished, odds are Chrome would eventually tell you the script was misbehaving.)

  • Firefox: Doesn't show "I'm here" but the UI does the same thing: Doesn't close the tab for several seconds after you request the tab closure.

  • IE11: May or may not show "I'm here", clicking the "close tab" button seems to respond more quickly (but still after several seconds) than Chrome.

It seems like it's fairly easy to go the click in before the script starts, so you do have to wait a moment after the page loads before clicking the close button.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Would you mind to put an alert right before closing `` and update the results with whether it is shown before tab is closed? – Aleksei Matiushkin Sep 15 '15 at 12:37
  • I think the reason to this behavior is the whole tab is, like Javascript execution, single-threaded. The "close tab" event is not completed until the js execution is not completed. – Pablo Lozano Sep 15 '15 at 12:40
  • 2
    @mudasobwa: `alert` does squirrelly things on browsers, and the squirrelly things vary from browser to browser. I wouldn't be at all surprised, for instance, if the loop completes, we call `alert`, the browser suspends the JavaScript thread (because `alert` is weird and blocking), and then processes the close event -- on some browsers, while doing something else on others. In particular when closing windows, remember scuzzy sites used to use `alert` and such to hijack your browser and keep you from leaving, so browsers are proactive around it. – T.J. Crowder Sep 15 '15 at 12:46
  • 1
    @Pablo: Yup, most browsers (this is not universal) not only process JavaScript on that thread, but also the UI chrome for the tab on it as well. – T.J. Crowder Sep 15 '15 at 12:47
  • @T.J.Crowder Yes, I see. Maybe `console.log` would work there? – Aleksei Matiushkin Sep 15 '15 at 13:00
  • 1
    @mudasobwa: Hard to see the console when the tab has been closed. But you could probably do something around writing to a separate window. You can't open a separate window during the main page parse (the OP's code happens during the main parse), but you might be able to get access to one that's already open via window name. – T.J. Crowder Sep 15 '15 at 13:08
  • 1
    @T.J.Crowder I finally realized that `LocalStorage` comes to the rescue here and yes, both Chrome and FF do write there before closing the tab. – Aleksei Matiushkin Sep 15 '15 at 13:13
  • @mudasobwa: Nice one! (Doh!) – T.J. Crowder Sep 15 '15 at 13:14