questio...">

Differences between window.onload / onunload and body.onload / onunload

I read the answers to the window.onload vs <body onload = "" /> question . This Q & A many claim window.onloadand body.onloadidentical. This is not what I am experiencing.

Consider two test pages:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head>
    <title>test 1</title>

   <script type="text/javascript">

   var initialSelectedIndex = 0;

   function resetMenu()
   {
      document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex;
   }

   </script>

</head>
<body onload="resetMenu();" onunload="resetMenu();">
   <br />
   <select id="fruitMenu">
      <option value ="apple">apple</option>
      <option value ="banana">banana</option>
      <option value ="strawberry">strawberry</option>
      <option value ="grape">grape</option>
   </select>

   <p><a href="http://www.google.com.au">google</a>
   </p>

</body>
</html>

and

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>test 2</title>

   <script type="text/javascript">

   var initialSelectedIndex = 0;

   function resetMenu()
   {
      document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex;
   }

   window.onload = resetMenu();
   window.onunload = resetMenu();

   </script>

</head>
<body>
   <br />
   <select id="fruitMenu">
      <option value ="apple">apple</option>
      <option value ="banana">banana</option>
      <option value ="strawberry">strawberry</option>
      <option value ="grape">grape</option>
   </select>

   <p><a href="http://www.google.com.au">google</a>
   </p>

</body>
</html>

Using the "1 test" page, if you select an item from the drop-down menu and click on the link to go from the page, and then click on the "Back" button, the menu will be reset. However, this does not happen on the page "test 2". Why?

, - - aspx RegisterStartupScript RegisterClientScriptBlock, " 1", onload/onunload, . OnLoad/OnUnload.

+3
1

, :

window.onload = resetMenu();

window.onload resetMenu, , onload ( ). :

window.onload = resetMenu;
window.onunload = resetMenu;

reset , ?

. onload:)

+2

Source: https://habr.com/ru/post/1725340/


All Articles