problem with the menu and the OnLoad event of BODY tag

Please note that official support for this menu version has now ceased. There are still plenty of users, though, and the forum is still running. Some of our long-time users may be able to help you out.
Post Reply
m_gamarra
Beginner
Beginner
Posts: 2
Joined: Wed Nov 06, 2002 7:43 pm

problem with the menu and the OnLoad event of BODY tag

Post by m_gamarra »

when do I use the menu, the onload event of the body is not called, how that to solve the this problem?
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

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
m_gamarra
Beginner
Beginner
Posts: 2
Joined: Wed Nov 06, 2002 7:43 pm

problem with the menu and the OnLoad event of BODY tag

Post by m_gamarra »

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.type == "text") || (field.elements.type == "textarea") || (field.elements.type.toString().charAt(0) == "s")) {
document.forms[0].elements.focus();
break;
}
}
}
}
//-->
</script>

</html>
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

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: Select all

<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
Post Reply