using <a href="a(this")>

Discussion in 'Javascript' started by Oliver Saunders, Oct 7, 2005.

  1. can someone please tell me why everytime i try and pass this to a js
    function i can't seem to use it for anything:

    <html>
    <head>
    <script language="javascript">
    function a(fromlink) {
    alert(fromlink.nodeName); // outputs undefined
    alert(fromlink.nodeType); // outputs undefined
    alert(fromlink.parentNode); // outputs undefined

    // i might as well be writing:
    alert(fromlink.blahblah_not_a_member);
    }
    </script>
    </head>
    <body>
    Some body text<br />
    <a href="javascript:a(this)">This is a link</a>
    </body>
    </html>

    what am i doing wrong? this is driving me mad
    Oliver Saunders, Oct 7, 2005
    #1
    1. Advertising

  2. Oliver Saunders wrote:


    > <a href="javascript:a(this)">This is a link</a>


    You want
    <a href="#"
    onclick="a(this); return false;">
    Inside of an event handler the this object is the element the event
    handler is attached to.
    A javascript: URL in a href attribute is not an event handler.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Oct 7, 2005
    #2
    1. Advertising

  3. Oliver Saunders

    Lee Guest

    Oliver Saunders said:
    >
    >can someone please tell me why everytime i try and pass this to a js
    >function i can't seem to use it for anything:
    >
    ><html>
    > <head>
    > <script language="javascript">
    > function a(fromlink) {
    > alert(fromlink.nodeName); // outputs undefined
    > alert(fromlink.nodeType); // outputs undefined
    > alert(fromlink.parentNode); // outputs undefined
    >
    > // i might as well be writing:
    > alert(fromlink.blahblah_not_a_member);
    > }
    > </script>
    > </head>
    > <body>
    > Some body text<br />
    > <a href="javascript:a(this)">This is a link</a>
    > </body>
    ></html>
    >
    >what am i doing wrong? this is driving me mad


    The "javascript:" pseudo-protocol functions similarly to the "http:"
    protocol, except that the new contents for the current window come
    from the output of your a() function, instead of from a web server.
    If the output happens to be void, then the contents are unchanged.
    You're abusing this mechanism for the side-effect of executing the
    function.

    Your a() function executes in the context of the window whose content
    it is replacing, so "this" is a reference to the window, not to the
    link. You can see that if you add the line:

    alert(fromlink);
    Lee, Oct 7, 2005
    #3
  4. > You want
    > <a href="#"
    > onclick="a(this); return false;">
    > Inside of an event handler the this object is the element the event handler is attached to.
    > A javascript: URL in a href attribute is not an event handler.


    ah huh thank you.

    > Your a() function executes in the context of the window whose content
    > it is replacing, so "this" is a reference to the window, not to the
    > link. You can see that if you add the line:
    >
    > alert(fromlink);



    yes actually I did notice that. thanks for clearing that up.
    Oliver Saunders, Oct 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,425
    Adrienne Boswell
    Jun 20, 2006
  2. Soren Vejrum
    Replies:
    4
    Views:
    515
    Lasse Reichstein Nielsen
    Jul 5, 2003
  3. saiho.yuen
    Replies:
    3
    Views:
    402
    kaeli
    Sep 14, 2004
  4. Replies:
    2
    Views:
    440
  5. kelvlam
    Replies:
    6
    Views:
    336
    kelvlam
    Jul 18, 2006
Loading...

Share This Page