Results 1 to 3 of 3

Thread: conflicting javascript

  1. #1
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default conflicting javascript

    Hey all,

    i've got 2 onclick functions that are conflicting. i'm not really sure how to add the event handlers, i've tried a whole bunch of things but obviously i'm missing something.

    my 2 function are:
    the first one is for horizontal scrolling, and is kept in a seperate js file called thw.js http://code.google.com/p/wplatformer...5e4d03a48bfc07


    the second one is a short function to change a tag's class name, used for links:
    Code:
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    var Lst;
    
    function CngClass(obj){
    if (Lst) Lst.className='';
    obj.className='selected';
    Lst=obj;
    }
    
    /*]]>*/
    </script></head>
    
    <body>
    
    <style type="text/css"> 
    #nav .selected { opacity:1.00;}
    
    
    </style> 
    <div id="nav"> 
    <a href="#home" id="hlink" onClick="CngClass(this);" style="margin-left:130px">home</a>
    <a href="#foamo" class="flink">foamo</a> 
    <a href="#book" class="blink">book</a>
    <a href="#portfolio" class="plink">portfolio</a>
    <a href="#contact" class="clink">contact</a> 
    </div>
    </body>
    wat i've got so far in terms of the event handler is:
    Code:
    <script type="text/javascript">
    	function addEventHandler(obj, eventName,handler){
    		if (document.attachEvent){
    	obj.attachEvent("on" + eventName, handler);
    		   } else if (document.addEventListener) {
          	obj.addEventListener(eventName, handler, false);
    	   	}
    	}
    </script>
    <script type="text/javascript">
    window.onload = myonload;
    function myonload(){
    addEventHandler(submit, "click" , function(){CngClass(obj)});
    		}
    </script>
    any help plz..

    mc
    Last edited by jscheuer1; 01-16-2011 at 11:45 PM. Reason: fix broken link

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This is, depending upon when it's called, OK for avoiding a conflict:

    Code:
    <script type="text/javascript">
    	function addEventHandler(obj, eventName,handler){
    		if (document.attachEvent){
    	obj.attachEvent("on" + eventName, handler);
    		   } else if (document.addEventListener) {
          	obj.addEventListener(eventName, handler, false);
    	   	}
    	}
    </script>
    But it should be reversed and use the ever present window object, not the document object which might not be available for testing (again depending upon when it's used) yet in some browsers:

    Code:
    <script type="text/javascript">
    	function addEventHandler(obj, eventName, handler){
    		if (window.addEventListener){
    			obj.addEventListener(eventName, handler, false);
    		}
    		else if (window.attachEvent){
    			obj.attachEvent('on' + eventName, handler);
    		}
    	}
    </script>
    Once you have that, you no longer need:

    Code:
    window.onload = myonload;
    Which does potentially expose itself to an onload conflict.

    You can do instead:

    Code:
    <script type="text/javascript">
    	function addEventHandler(obj, eventName, handler){
    		if (window.addEventListener){
    			obj.addEventListener(eventName, handler, false);
    		}
    		else if (window.attachEvent){
    			obj.attachEvent('on' + eventName, handler);
    		}
    	}
    </script>
    <script type="text/javascript">
    function myonload(){
    addEventHandler(submit, "click" , function(){CngClass(obj)});
    		}
    addEventHandler(window, 'load', myonload);
    </script>
    But this makes no sense unless submit is previously defined, which it's not in any of the code you are showing. And the obj argument in:

    Code:
    addEventHandler(submit, "click" , function(){CngClass(obj)});
    can never be defined when called in that way.

    If you want more help:

    Please post a link to a page on your site that contains the problematic code so we can check it out.

    And since you seem to imply that each of these scripts work just fine on their own, post a link to two more pages:

    1. One where you have the thw.js working.

    2. Another where you have this CngClass code or whatever it is working.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    wow thats all a bit confusing. let me fix and tidy up my code before i post again as I noticed there was some incorrect code in my last post.

    Thanks though

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
  •