SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem setting a event listener

    I have this code here to add a link to a css element that will execute the function 'popup.exit()' when the user clicks it, I know I could set the href attribute of the link to 'javascript: popup.exit()' but I would rather set an event handler as it makes my code cleaner. This is the code I am trying:

    Code:
    // Add the close ability to the popup
    exitNode = document.createElement('a');
    exitText = document.createTextNode(popup.exitText);
    exitNode.appendChild(exitText);
    exitNode.setAttribute('href', '#');
    exitNode.addEventListener('click', popup.exit(), false);
    exitNode.className = popup.exitClass;
    popup.dObj.appendChild(exitNode);
    And this is the error I'm getting from the console of Firebug in FF3:

    Code:
    uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"
    it is pointing to this line:

    Code:
    exitNode.addEventListener('click', popup.exit(), false);

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are passing the return value of popup.exit()- lose the function call parentheses.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Try

    Code javascript:
    exitNode.addEventListener('click', popup.exit, false);

    If you want to keep IE happy you can also include an attachEvent for it.

    Even better, if you won't need more than one click event on the exit node (which is highly unlikely) you can use the traditional event registration technique instead.

    Code javascript:
    exitNode.onclick = popup.exit;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for the help, it was the parentheses that were causing me problems.... and yes IE support was the next on my list... I just got tired of rooting through the code to find the entire "<a href='javascript:...." in my code. And I'm finally taking the time to properly learn javascript instead of just hacking stuff together from examples like I have been


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
  •