470,636 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Using THIS in href

How come I can do this:
<a onclick="javascript:myfunc(this)">blah</a>

but I cannot do this:
<a href="javascript:myfunc(this)">blah</a>

For some reason the THIS reference doesn't work when used in href.

Any help?
Jul 20 '05 #1
4 19035
On 22 Jan 2004 10:59:42 -0800, Radioactive Man <up***********@yahoo.com>
wrote:
How come I can do this:
<a onclick="javascript:myfunc(this)">blah</a>

but I cannot do this:
<a href="javascript:myfunc(this)">blah</a>

For some reason the THIS reference doesn't work when used in href.

Any help?


Don't use JavaScript pseudo-protocol URIs. It's a simple as that.

If it works with the intrinsic event, why are you worried anyway?

JavaScript URIs are generally discouraged because they offer no way to
specify alternative content for users that have JavaScript disabled on
their systems. Also, as you have discovered, the pseudo-protocol can cause
problems that can be avoided by using another approach.

Unless you *really* need to use a JavaScript URI[1] in a link, use the
onclick event.

Mike
[1] I think anything that can be performed through a JavaScript URI can be
acheived some other way, though it might take more work.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Lee
Radioactive Man said:

How come I can do this:
<a onclick="javascript:myfunc(this)">blah</a>

but I cannot do this:
<a href="javascript:myfunc(this)">blah</a>

For some reason the THIS reference doesn't work when used in href.


The HREF attribute isn't an event handler of the link,
and shouldn't be abused to make it try to behave as one.
The script doesn't execute within a scope in which "this"
has any useful meaning.

Try:

<a href="#someValidURL" onclick="myfunc(this);return false">

Jul 20 '05 #3
up***********@yahoo.com (Radioactive Man) writes:
How come I can do this:
<a onclick="javascript:myfunc(this)">blah</a>
You can do this too:
<a onclick="myfunc(this);">blah</a>
And you should. No need to add a label to the script if you don't use it.
but I cannot do this:
<a href="javascript:myfunc(this)">blah</a>
Good. Don't. <URL:http://jibbering.com/faq/#FAQ4_24>
For some reason the THIS reference doesn't work when used in href.


The code you write in the intrinsic event handler (onclick) is executed
as if it is the body of a method on the element object. That means that
"this" refers to the element object.

The in the href is placed in location.href, recognized as a Javascript
protocol URI, and attempted executed in the scope of the entire page.
That means that "this" most likely points to the window object.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Michael Winter <M.******@blueyonder.co.invalid> writes:
[1] I think anything that can be performed through a JavaScript URI
can be acheived some other way, though it might take more work.


I know of only one excpetion: Bookmarklets. Which is a great use for
it too, and the only one I would recommend.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JP SIngh | last post: by
7 posts views Thread by johkar | last post: by
4 posts views Thread by Japhy | last post: by
9 posts views Thread by Nathan Sokalski | last post: by
8 posts views Thread by =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.