473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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

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
3 1723


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

Similar topics

6
by: lecichy | last post by:
Hello. Well, I found this piece of code on php.net. Thats fine but where can i find explanation for all these ("|\')?(*)("|\')?.*>(*)' syntax so that I can construct my own rules for all kind...
14
by: Gregory | last post by:
Hello, I'm trying to do the above in order to process an image and return the result to an html image control. It fails and my key suspects are either the variable that I'm passing in -...
1
by: Jesper Hermansen | last post by:
Hi! I'm making a system that will generate Winamp-playlists. To make it easy for the user to add a file to the list, I'm using <input type="file">. The problem with this is that I only get...
10
by: Dieter Salath? | last post by:
Hi, in our webpage, a user could open a windows explorer to his temp directory with a simple link and usage of the file protocol: <a href="file://C:\temp" target="_blank">C:\temp</a> This...
10
by: Gernot Frisch | last post by:
Hi, I'm currently writing: <span onclick="window.open(...);">Klick Here</span> but I want to use the <a href> for this, since it is defined in the css script the way I want my link to open....
7
by: Jim in Arizona | last post by:
Using this: <asp:ListItem Value="0"</asp:ListItem> as long as I have a value listed and leave the area between the tags blank, when viewed the DropDownList will show a 0 as a choice. I want it...
1
by: mark4asp | last post by:
<form runat="server"automatically adds <divtag to code contained within. Is there a way to stop that? Mixing block-level elements with inline-level elements messes up the HTML becasuse that is...
10
by: vunet.us | last post by:
What is the workaround of passign a parameter to any included asp file: <!-- #Include File="file.asp" --> This obviously does not work: <!-- #Include File="file.asp?id=123" --> Thank you
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.