By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Oct 7 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


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/
Oct 7 '05 #2

P: n/a
Lee
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);

Oct 7 '05 #3

P: n/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.
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.
Oct 7 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.