2

In my tests, JavaScript's reload() function seems to prevent code that comes after it from executing... but can anybody confirm that this is true 100% of the time?

Assume the user has JavaScript enabled with no fancy tricks.

That is:

<head>
    <!-- Comes first in the <head>, and could be included via an external .js file. -->
    <script type="text/javascript">
        window.location.reload(true);
    </script>
    <!-- ... -->
</head>
<body>
    <!-- ... -->
    <!-- Later in the code.  This could be js, html... or anything else. -->
    <script type="text/javascript">
        alert('this should NEVER display... regardless of OS/browser');
    </script>
    <!-- ... -->
    <span class="you-cant-see-me">secret!</span>
    <!-- ... -->
</body>
Peter
  • 4,021
  • 5
  • 37
  • 58
  • Although... this specific example seems to show the alert (but not the text) in IE... – Peter Dec 07 '11 at 06:30
  • 1
    What seems to happen in above case is, even before the second script tag is parsed, the reload is happening. I tried reload() in a button click and wrote some code after that, and the code did execute. – Pawan Dec 07 '11 at 06:37
  • But why does the browser parse the second script tag? Shouldn't the browser *immediately* reload the page? – Peter Dec 07 '11 at 06:42
  • may be the browser treats both the script tags as one chunk of code and tries to execute everything until it is able to(as if the code is part of one function). If you have noticed as per your first comment, you will be able to see the alert but broswer wont wait till you click the ok button and page will reload. – Pawan Dec 07 '11 at 07:00
  • Related: [Can we have code after location.reload(true)?](/q/52192666/4642212), [Why is the content not showing after refresh](/q/57778178/4642212). – Sebastian Simon Oct 21 '22 at 22:39

0 Answers0