www.webdeveloper.com
Results 1 to 14 of 14

Thread: [RESOLVED] onload bugs everything

  1. #1
    Join Date
    Oct 2007
    Posts
    89

    resolved [RESOLVED] onload bugs everything

    I have a click menu, which you can find here: http://www.stunicholls.com/menu/simple.html

    The problem is that to have this menu working you must add onload="clickMenu('menu')" to <body> tag, otherwise the menu won't work. But if I add this to "the body" other functions on the page won't work. Is there a way to activate the menu, without adding onload="clickMenu('menu')" to the body tag?

    Thanks, Yan

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    For obvious security reasons I don't click on posted links in forums like these. But is sounds like you are either accidentally 'blocking out' other onload functions when you add the clickMenu() to the body onload, and thus stopping the other functions from working, or clickMenu() itself is stopping other functions from working properly somehow.

    If clickMenu() is interfering with other functions then it probably won't matter where or how you start clickMenu() and you would need to figure out what might be wrong with clickMenu().

  3. #3
    Join Date
    Feb 2010
    Posts
    73
    try firebug for FF and check if theres no errors

  4. #4
    Join Date
    Oct 2007
    Posts
    89
    No, nothing in firebug... here's the js, try it out:

    Code:
    clickMenu = function(menu) {
    	var getEls = document.getElementById(menu).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";
    				}
    			}
    		}
    	}

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WLQ View Post
    Is there a way to activate the menu, without adding onload="clickMenu('menu')" to the body tag?

    Thanks, Yan
    Yes. Call in onload (window.onload) from within a javascript code
    Code:
    <script type="text/javascript">
    ...//
    onload=function(){
    clickMenu('menu');
    //.. other functions, statements, etc.
    }
    </script>

  6. #6
    Join Date
    Oct 2007
    Posts
    89
    no, didn't help, I guess it has something to do with the self javascript, someone knows of any other good "click" menus?

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WLQ View Post
    no, didn't help, I guess it has something to do with the self javascript, someone knows of any other good "click" menus?
    Should. Unless you have other libraries or pieces of code which use window.onload as well.

    You could also try to attach new handlers:
    Code:
    function AttachEvent(obj,evt,fnc,useCapture){
    	if (!useCapture) useCapture=false;
    	if (obj.addEventListener){
    		obj.addEventListener(evt,fnc,useCapture);
    		return true;
    	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
    	else{
    		MyAttachEvent(obj,evt,fnc);
    		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
    	}
    } 
    function MyAttachEvent(obj,evt,fnc){
    	if (!obj.myEvents) obj.myEvents={};
    	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
    	var evts = obj.myEvents[evt];
    	evts[evts.length]=fnc;
    }
    
    function MyFireEvent(obj,evt){
    	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
    	var evts = obj.myEvents[evt];
    	for (var i=0,len=evts.length;i<len;i++) evts[i]();
    }
    
    AttachEvent(window,'load',function(){
    clickMenu('menu');
    //.. other functions, statements, etc.
    },false);

  8. #8
    Join Date
    Oct 2007
    Posts
    89
    that didn't help either, but looking through my js files, I've found out that I do have another window.onload function

  9. #9
    Join Date
    Oct 2007
    Posts
    89
    any other way I can call my menu, other then window.onload?

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WLQ View Post
    that didn't help either
    No way. Should work by all means. You must have done another error... Without seeing your code I can not say which error. Have you removed onload=clickMenu('menu') from the BODY tag?

    Post a link to a test page, to see what is all about.

  11. #11
    Join Date
    Oct 2007
    Posts
    89
    I did remove it from the body tag, and added the javascript you gave in the head. Menu will work, but my search field won't work.

    Should. Unless you have other libraries or pieces of code which use window.onload as well.
    Js file for search begins with "window.onload=function() {"

  12. #12
    Join Date
    Oct 2007
    Posts
    89
    could that be a problem?

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WLQ View Post
    could that be a problem?
    No. Try something else. If you say that your search JS file has something like:
    Code:
    window.onload=function(){
    //... the rest of the code for search
    }
    put your menu starting function inside that anonymous:
    Code:
    window.onload=function(){
    clickMenu('menu');
    //... the rest of the code for search
    }

  14. #14
    Join Date
    Oct 2007
    Posts
    89
    yes, great, thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles