Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sliding menus open on load

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
I've got this little sliding dhtml menu thing, and am trying to have one submenu open onload. I can't figure out how. I tried putting one of the triggers in the body tag on load, but can't get it to work. Any help is appreciated. The simplified code is below.

<html>
<head>
<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html; charset=iso-8859-1&quot;>
<title></title>
<style>
<!--
DIV.clTop{position:absolute; width:130}
DIV.clSub{position:absolute; left:10; width:130}
#divCont{position:relative; left:0; top:0; height:600; width:130;}
#divMain{position:absolute}
//-->
</style>
<script language=&quot;JavaScript&quot;>
<!--
/************************************************************************************
Change this to false if you want all the submenus to get unfold when you
foldout a new one.
************************************************************************************/
var stayFolded=false

/************************************************************************************
Browsercheck
************************************************************************************/
var n = (document.layers) ? 1:0;
var ie = (document.all) ? 1:0;
var browser=((n || ie) && parseInt(navigator.appVersion)>=4)

/************************************************************************************
Making cross-browser objects
************************************************************************************/
function makeMenu(obj,nest){
nest=(!nest) ? '':'document.'+nest+'.'
this.css=(n) ? eval(nest+'document.'+obj):eval('document.all.'+obj+'.style')
this.ref=(n) ? eval(nest+'document.'+obj+'.document'):eval('document');
this.height=n?this.ref.height:eval(obj+'.offsetHeight')
this.x=(n)? this.css.left:this.css.pixelLeft;this.y=(n)? this.css.top:this.css.pixelTop;
this.hideIt=b_hideIt; this.showIt=b_showIt; this.vis=b_vis; this.moveIt=b_moveIt
return this
}
function b_showIt(){this.css.visibility=&quot;visible&quot;}
function b_hideIt(){this.css.visibility=&quot;hidden&quot;}
function b_vis(){if(this.css.visibility==&quot;hidden&quot; || this.css.visibility==&quot;hide&quot;) return true;}
function b_moveIt(x,y){this.x=x; this.y=y; this.css.left=this.x; this.css.top=this.y}
/************************************************************************************
Initiating the page. Just add to the arrays here to get more menuitems
and add divs in the page
************************************************************************************/
function init(){
oTop=new Array()
oTop[0]=new makeMenu('divTop1','divCont')
oTop[1]=new makeMenu('divTop2','divCont')
oTop[2]=new makeMenu('divTop3','divCont')
oTop[3]=new makeMenu('divTop4','divCont')
oSub=new Array()
oSub[0]=new makeMenu('divSub1','divCont.document.divTop1')
oSub[1]=new makeMenu('divSub2','divCont.document.divTop2')
oSub[2]=new makeMenu('divSub3','divCont.document.divTop3')
oSub[3]=new makeMenu('divSub4','divCont.document.divTop4')
for(i=0;i<oSub.length;i++){ oSub.hideIt() }
for(i=1;i<oTop.length;i++){ oTop.moveIt(0,oTop[i-1].y+oTop[i-1].height) }
}
/************************************************************************************
This is the function that changes the sub menus to folded or unfolded state.
************************************************************************************/
function menu(num){
if(browser){
if(!stayFolded){
for(i=0;i<oSub.length;i++){
if(i!=num) oSub.hideIt()
}
for(i=1;i<oTop.length;i++){
oTop.moveIt(0,oTop[i-1].y+oTop[i-1].height)
}
}
oSub[num].vis()?oSub[num].showIt():eek:Sub[num].hideIt()
for(i=1;i<oTop.length;i++){
if(!oSub[i-1].vis()) oTop.moveIt(0,oTop[i-1].y+oTop[i-1].height+oSub[i-1].height)
else oTop.moveIt(0,oTop[i-1].y+oTop[i-1].height)
}
}
}
//Initiating the menus onload, if it's a 4.x+ browser.
if(browser) onload=init;

function MM_showHideLayers() { //v6.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
obj.visibility=v; }
}
//-->
</script>
</head>
<BODY>
<div id=&quot;divCont&quot;>
<div id=&quot;divTop1&quot; class=&quot;clTop&quot; style=&quot;&quot;> <a href=&quot;javascript:;&quot; onclick=&quot;menu(0); return false&quot;>
test1</a><br>

<div id=&quot;divSub1&quot; class=&quot;clSub&quot; style=&quot;&quot;>
<p>test1 </p>
</div>
</div>
<div id=&quot;divTop2&quot; class=&quot;clTop&quot; style=&quot;&quot;> <a href=&quot;javascript:;&quot; onclick=&quot;menu(1); return false&quot;>
test2</a> <br>

<div id=&quot;divSub2&quot; class=&quot;clSub&quot; style=&quot;&quot;> test2
</div>
</div>
<div id=&quot;divTop3&quot; class=&quot;clTop&quot; style=&quot;&quot;> <a href=&quot;javascript:;&quot; onclick=&quot;menu(2); return false&quot;>
test3</a> <br>

<div id=&quot;divSub3&quot; class=&quot;clSub&quot; style=&quot;&quot;>
<p>test3 </p>
</div>
</div>
<div id=&quot;divTop4&quot; class=&quot;clTop&quot; style=&quot;&quot;> <a href=&quot;javascript:;&quot; onclick=&quot;menu(3); return false&quot;>
test4</a> <br>

<div id=&quot;divSub4&quot; class=&quot;clSub&quot; style=&quot;&quot;>
<p>test4</p>
</div>
</div>
</div>
</body>
</html>
 
The problem is that the body's onload event is being set programmatically (look for the line that contains the text &quot;onload=init&quot;). The good news is, the function that's being called, init(), is also defined on the page. Put whatever javascript statements you want to execute onload at the end of the function definition for the init() function, and they should run when the page loads.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top