473,536 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6834
by: codeslayer | last post by:
Greetings to everyone in ‘forum-land': I have a problem that has plaguing me to no end. It is a CSS-related question, and I have not seen this question posted anywhere in forums or through Google search. So here is my problem: I want to use CSS to apply images as bullet styles. However, I want to be able to apply VARIOUS, MULTIPLE...
3
7628
by: JP SIngh | last post by:
Hi All I have users who upload files using my application using ASPUPLOAD component. My code uploads the file to a network location and once the upload is finish I display the hyperlink using the following code <a href=\myserver\attachments\<%=server.urlencode(rs("FileName"))%> target="_blank" ><%=rs("Filename")%>
7
2968
by: johkar | last post by:
I am confused on childNodes or children. When you have nested lists, I would think it would all be one list in the Dom, this doesn't seem to be the case. So how do you traverse an unordered list? Any help appreciated. John In the below script, I can expand and contract the nodes when clicked on, but I want the menu to close all other...
4
2702
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display .="<li>".$wohdate."</li>" ; $TemplateText = Replace($TemplateText,"@$wohdisplayndx@",$woh_display);
14
1963
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm just trying to do this in phases to understand what's going on. I'm getting an error (Object doesn't support this property or method) in IE and I...
9
2105
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0's ImageMap Control to create 2 imagemaps, one directly below the other. When I do this a thin blank space appears between them. After several days of frustration I realized that the difference between what the ImageMap Control outputs and the html on the page I was trying to reproduce with ASP.NET 2.0 was the following:...
3
4543
by: MLD | last post by:
PLEASE HELP! I would like to include a UL as a menu, styled by an included CSS Style Sheet. The problem I am having is how do I dynamically set the "active" page class using JavaScript to change each time I click on a new list item? I have found a bunch of examples but none that really accomplish what I need.
8
8925
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then throws up the following error in the browser: CS1061: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Web' and no extension...
1
3435
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i have to made in my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
12
77938
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please give me some idea where I am going wrong ?? I just want to retrieve data from my emp_mstr table and display it using my JSP file... The table...
0
7289
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7273
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7637
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3342
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
918
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.