473,396 Members | 2,011 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,396 software developers and data experts.

return false not preventing href action

I've read that one must return false from an onclick handler attached
to an anchor if we wanted to prevent the browser from following the
href. In a greasemonkey script I'm hacking together with duct tape and
chicken wire the links that I'm creating follow the hrefs, killing the
associated action. To get around this, I'm assigning the URL which I
need in the logMe function to a made up attribute, rather than use
newlink.href as I do below.

In newlink below, my browser (Firefox) will follow the href despite my
returning false. What did I do wrong? Thanks

for (linkIndex = 0; linkIndex < doclinks.snapshotLength; linkIndex++)
{
var origlink = doclinks.snapshotItem(linkIndex);
if (origlink.href.match(myRE))
{
newlink = document.createElement('a');
newlink.innerHTML = " [ EXPAND ] ";
newlink.id = preamble + ":" + linkIndex;
newlink.href = origlink.href;
newlink.addEventListener('click', function(event){ logMe(event);
return false; }, false);
origlink.parentNode.insertBefore(newlink,origlink. nextSibling);
}
}

Sep 24 '05 #1
1 2214
ja************@gmail.com wrote:
I've read that one must return false from an onclick handler
attached to an anchor if we wanted to prevent the browser
from following the href.
That is true if the handler is assigned to the - onclick - property of
the elements.

<snip> newlink.addEventListener('click', function(event){ logMe(event);

<snip>

This is not assigning the handler to the - onclick - property of the
elements. It is assigning an event listener to the element using the W3C
Events DOM standard method. To cancel the default action (the navigation
to the HREF) from an event listener you call the - preventDefault -
method of the event object.

You would probably be better off assigning the function to - onclick -
property of the element, as that is much more cross-browser and just
returning false form that handler is reliable/sufficient:-

newlink.onclick = function(ev){
logMe((ev || window.event));
return false;
};

Assuming the intention is to only assign one event handler.

Though using an inner function may not be the best idea.

Richard.
Sep 25 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: w i l l | last post by:
Why does this work the way it does? If someone could explain return true, and return false to me I'd greatly appreciate it. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
1
by: Colin Hale | last post by:
I have a problem (amongst others)... I have the following code which sends 2 emails. I only want to send one! <A ONCLICK="OnActionClick('E-Mail')" ID="EMailNowAnchor" NAME="EMailNowAnchor"...
7
by: sindre | last post by:
Hi, Some place I use links to submit forms instead of a submit button. The way I have done this is: <a href="javascript:document.getElementById('<?php print "delete$i"...
13
by: kurtj | last post by:
Hello Gurus: I have a validation script (below) that is somehow messed up. If the Name field is blank, I get the alert message, then the browser window goes to a blank document with the word...
1
by: mailpitches | last post by:
X-No-Archive: yes a function, the return value of the function is a boolean. What does this boolean value mean? Example: <body onload="document.getElementById('field').onkeydown=function(x) {...
4
by: ameyas | last post by:
hi all, i am using an image which has image map and i provide the href action url. i have provided the url to open a pop up window like "javascript:window.open('www.google.com');" but on...
4
by: cssExp | last post by:
hello, i want to check for errors on submitting a form, on encountering an error it runs a function and returns a false preventing form submit. I have implemented that. function eCheck() { ...
1
by: Sudarhan | last post by:
Hello frnds I have created a webbased form using asp and javascript .. while submitting the form i am validating the fields in the form .it validates the field and returns alert message. but when...
5
by: GiJeet | last post by:
Hello, I'm trying to figure this code out. <input type="submit" onclick="if(!confirm('Are you sure?') return false; "... /> I know that if you return false in an event like this onclick it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
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...
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,...

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.