Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferences between window.onload/onunload and body.onload/onunload
    primarykey
    data
    text
    <p>I have read the answers for the question <a href="https://stackoverflow.com/questions/191157/window-onload-vs-body-onload">window.onload vs &lt;body onload=""/&gt;</a>. In that Q&amp;A, many claim that <code>window.onload</code> and <code>body.onload</code> are identical. This is not what I experience.</p> <p>Consider the two test pages:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head&gt; &lt;title&gt;test 1&lt;/title&gt; &lt;script type="text/javascript"&gt; var initialSelectedIndex = 0; function resetMenu() { document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex; } &lt;/script&gt; &lt;/head&gt; &lt;body onload="resetMenu();" onunload="resetMenu();"&gt; &lt;br /&gt; &lt;select id="fruitMenu"&gt; &lt;option value ="apple"&gt;apple&lt;/option&gt; &lt;option value ="banana"&gt;banana&lt;/option&gt; &lt;option value ="strawberry"&gt;strawberry&lt;/option&gt; &lt;option value ="grape"&gt;grape&lt;/option&gt; &lt;/select&gt; &lt;p&gt;&lt;a href="http://www.google.com.au"&gt;google&lt;/a&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head&gt; &lt;title&gt;test 2&lt;/title&gt; &lt;script type="text/javascript"&gt; var initialSelectedIndex = 0; function resetMenu() { document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex; } window.onload = resetMenu(); window.onunload = resetMenu(); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;br /&gt; &lt;select id="fruitMenu"&gt; &lt;option value ="apple"&gt;apple&lt;/option&gt; &lt;option value ="banana"&gt;banana&lt;/option&gt; &lt;option value ="strawberry"&gt;strawberry&lt;/option&gt; &lt;option value ="grape"&gt;grape&lt;/option&gt; &lt;/select&gt; &lt;p&gt;&lt;a href="http://www.google.com.au"&gt;google&lt;/a&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>With the "test 1" page, if you select an item from the drop down menu and the click the link to navigate away from the page and then hit the back button the menu will be reset to its initial state. This doesn't happen for the "test 2" page though. Why?</p> <p>While this is a test, my goal is to do something similar on an aspx page using RegisterStartupScript or RegisterClientScriptBlock so I want to be able to recreate the behaviour of "test 1" without using the body onload/onunload but using window.onload/onunload.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload