SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    185
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <body onload="clickNav('nav')"> issues.

    hello, ive got a menu that uses <body onload="clickNav('nav')"> but somehow won't display the submenu.is there way to insert, the onload in the body tag, the js scrpit itself while avoiding having it in the body tag.?
    Code:
    clickNav = function(nav) {
    	var getEls = document.getElementById(nav).getElementsByTagName("LI");
    	var getAgn = getEls;
    
    	for (var i=0; i<getEls.length; i++) {
    			getEls[i].onclick=function() {
    				for (var x=0; x<getAgn.length; x++) {
    				getAgn[x].className=getAgn[x].className.replace("unclick", "");
    				getAgn[x].className=getAgn[x].className.replace("click", "unclick");
    				}
    			if ((this.className.indexOf('unclick'))!=-1) {
    				this.className=this.className.replace("unclick", "");;
    				}
    				else {
    				this.className+=" click";
    				}
    			}
    		}
    	}
    many thanks.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by moisea View Post
    is there way to insert, the onload in the body tag, the js scrpit itself while avoiding having it in the body tag.?
    If I understand what you're saying correctly, instead of having the code in the onload of the body tag, you can place the code at the end of the body instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by moisea View Post
    is there way to insert, the onload in the body tag, the js scrpit itself while avoiding having it in the body tag.?
    You can do it like this as well:
    Code javascript:
    window.onload = function(e){
    	clickNav('nav');
    }
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    185
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks pmw57, yes that what i am trying to do.
    thanks rajug, do i put the codes
    Code:
    window.onload = function(e){
        clickNav('nav');
    }
    at the beginning or end of the js?
    Code:
    clickNav = function(nav) {
    	var getEls = document.getElementById(nav).getElementsByTagName("LI");
    	var getAgn = getEls;
    
    	for (var i=0; i<getEls.length; i++) {
    			getEls[i].onclick=function() {
    				for (var x=0; x<getAgn.length; x++) {
    				getAgn[x].className=getAgn[x].className.replace("unclick", "");
    				getAgn[x].className=getAgn[x].className.replace("click", "unclick");
    				}
    			if ((this.className.indexOf('unclick'))!=-1) {
    				this.className=this.className.replace("unclick", "");;
    				}
    				else {
    				this.className+=" click";
    				}
    			}
    		}
    	}
    window.onload = function(e){
        clickNav('nav');
    }
    like this?
    many thanks guys.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    There is a good run-down of the different options available and their implications, in this recent thread.
    http://www.sitepoint.com/forums/showthread.php?t=527863
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    185
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you paul, i tried tried tried nothing seems to work can't make it work. many thanks anyway for your reply. appreciated it .
    many thanks.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    If you put up a test page we will help you to debug it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    185
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks works fine with onload in the body tag
    many thanks.
    Last edited by moisea; Jan 28, 2008 at 11:31.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •