Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Archived Topics for the old Version 3.0 JavaScript Menu
Forum Topic: Click to view post
Last Updated: Wednesday July 18 2012 - 06:07:18

problem with the menu and the OnLoad event of BODY tag


Poster: m_gamarra
Dated: Wednesday November 6 2002 - 18:47:32 GMT

when do I use the menu, the onload event of the body is not called, how that to solve the this problem?


Poster: kevin3442
Dated: Wednesday November 6 2002 - 20:11:38 GMT

There's something more going on than simply using the menu system. The menus do not normally affect onLoad events (BODY or elsewhere). Could you post a URL to your site, so we could have a closer look?

Kevin

problem with the menu and the OnLoad event of BODY tag


Poster: m_gamarra
Dated: Thursday November 7 2002 - 12:37:18 GMT

unhappily I cannot publish my url.
But it follows an example showing the problem.
Alternate the position of the script of the menu for before or after the body and you looked at the bug.

<html>
<head>
<title></title>
</head>

<script language=javascript src="menujavascript/menu_array.js" type=text/javascript></script>
<script language=javascript src="menujavascript/mmenu.js" type=text/javascript></script>

<body onLoad="placeFocus()">
<!--
<script language=javascript src="menujavascript/menu_array.js" type=text/javascript></script>
<script language=javascript src="menujavascript/mmenu.js" type=text/javascript></script>
//-->

<form name="form" method="post">
<input type="text" name="text0">
<input type="text" name="text1">
</form>
</body>


<script language=javascript>
<!--
function placeFocus() {
//alert("placeFocus")
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
}
}
}
}
//-->
</script>

</html>


Poster: kevin3442
Dated: Friday November 8 2002 - 0:39:47 GMT

Hi,

My first impression on a quick glance is that it would be unusual to place <SCRIPT>s outside of the <HEAD> or <BODY>. See for example, this page. Of the two approaches you've represented for loading the menu scripts, the first places the <SCRIPT> after the </HEAD> but before the <BODY>. I would not recommend this. In fact, I've only seen that done when loading or defining scripts between the closing </HEAD> and an opening <FRAMESET>.

I also notice that your placeFocus() function is defined in a <SCRIPT> that occurs after the closing </BODY>. Again, unusual. Since the function is defined at the bottom of the document, after the </BODY>, the browser may not be aware of the function's existence when the body's onLoad event is triggered. In other words, when the body is finished loading, the placeFocus() function does not even exist yet, since the browser has not gotten to its definition yet; so it can't be called when onLoad is triggered. Have you gotten any error messages to the effect that the function is undefined? If you place the function definition inside the <HEAD>, then you guarantee that the function exists when the body loads.

I would recommend the following. (1) Place the two <SCRIPT>s that load the menus inside of the <BODY>; that seems to be the preferred approach discussed in various places in this forum. (2) Place the <SCRIPT> that defines your placeFocus() function inside the <HEAD>. Like so:
Code:
<html>
<head>
<title></title>
<script language=javascript>
<!--
function placeFocus() {
//alert("placeFocus")
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i = 0; i < field.length; i++) {
      if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
        document.forms[0].elements[i].focus();
        break;
      }
    }
  }
}
//-->
</script>
</head>

<body onLoad="placeFocus()">
<!--
<script language=javascript src="menujavascript/menu_array.js" type=text/javascript></script>
<script language=javascript src="menujavascript/mmenu.js" type=text/javascript></script>
//-->

<form name="form" method="post">
<input type="text" name="text0">
<input type="text" name="text1">
</form>
</body>
</html>

Try uncommenting the "alert" test you must've put in earler, to see if you now get the alert when your page loads.

Hope that helps,

Kevin