SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast trat02community's Avatar
    Join Date
    Sep 2005
    Location
    Slovenia - Europe
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Link fires on onload instead onclick

    Link fires on onload instead onclick. i just don't get it why!!
    So, the link fires first time page loads, and then doesen't work any more?!

    demoScript.js
    Code:
    var myO = {  	
      init: function(){  		
        var mojLink = document.getElementById('prviLink');									//dobimo referenco na link na strani  		
        SitePoint.addEvent(mojLink, 'click', alert('haha'), false);	//kičemo APIjevo funkcijo, za dodajat evente  	
      }  
    };    /* Zaženemo funkcijo */  
    SitePoint.addEvent(window, 'load', myO.init, false);
    sitePoint_addEvent.js
    Code:
    var SitePoint = {  	
      addEvent: function(elm, evType, fn, useCapture){
    		if(elm.addEventListener)
    		{
    			elm.addEventListener(evType, fn, useCapture);
    			return true;
    		}
    		else if(elm.attachEvent)
    		{
    			var r = elm.attachEvent('on'+ evType, fn);
    			return r;
    		}
    		else
    		{
    			elm['on'+ evType] = fn;
    		}
    	}//end addEvent
    };
    demo.htm
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>SitePoint_addEvent</title>
    
      <script language="javascript" src="JavaScript_API/sitepoint_addEvent.js"></script>
      <script language="javascript" src="myJavaScript/demoScript.js"></script>
      </head> 
       <body>
        <a href="#" id="prviLink">Stestiraj link</a>
       </body>
      </html>
    I see this error mesage in javascript console in Firefox:
    :Napaka: [Exception... "'JavaScript component threw a null value as an exception' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001d (NS_ERROR_XPC_JS_THREW_NULL)" location: "<unknown>" data: no]

    I'm loosing my nerves with this...

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    alert('haha') will run straight away and the result returned from the alert (null) will be the function to run onclick. To fix it replace that code with function() {alert('haha');}
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast trat02community's Avatar
    Join Date
    Sep 2005
    Location
    Slovenia - Europe
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked ,tnx!
    And I wrote this down this time


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
  •