469,588 Members | 2,875 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

onclick event wierdness

Consider the following (where 'a' is an anchor element with a valid URL
href):

a.onclick=help_mode;
function help_mode() { alert("hi"); return false; }

As expected clicking on the link now results in a "hi" but no change in
location, the link isn't followed because onclick returned false. Now
look at what I thought was the same thing:

a.addEventListener('click', help_mode, false);
function help_mode() { alert("hi"); return false; }

Clicking on the link now results in "hi" and then the link is loaded!
Can anyone explain what the difference is, that the return value of
false is ignored when addEventListener is used?

I'm trying to disable links for every anchor on the page with
javascript, and instead launch a "help" window based on the title
attribute of the tag. In other words, a help mode where the user can
click on the element they want help with, but I don't want the location
to change when in help mode.

HS

Nov 12 '06 #1
8 1444

ho**********@gmail.com wrote:
Consider the following (where 'a' is an anchor element with a valid URL
href):

a.onclick=help_mode;
function help_mode() { alert("hi"); return false; }

As expected clicking on the link now results in a "hi" but no change in
location, the link isn't followed because onclick returned false. Now
look at what I thought was the same thing:

a.addEventListener('click', help_mode, false);
function help_mode() { alert("hi"); return false; }

Clicking on the link now results in "hi" and then the link is loaded!
Can anyone explain what the difference is, that the return value of
false is ignored when addEventListener is used?

see stopPropogation() and preventDefault() for DOM 2 events. See
cancelBubble and returnValue for the IE event model.

Note that some versions of Safari, I think all versions less than 2.0,
and some other browsers cannot stop the link being followed if you are
using DOM 2 type handlers. preventDefault doesn't work in these
browsers for at least the onclick event handlers. This was a bug and
the workaround is to use the old style event attributes like your first
try.

Peter

Nov 12 '06 #2
Peter Michaux wrote:
>
see stopPropogation() and preventDefault() for DOM 2 events. See
cancelBubble and returnValue for the IE event model.

Note that some versions of Safari, I think all versions less than 2.0,
and some other browsers cannot stop the link being followed if you are
using DOM 2 type handlers. preventDefault doesn't work in these
browsers for at least the onclick event handlers. This was a bug and
the workaround is to use the old style event attributes like your first
try.
Now I remember why I hate writing javascript. Thanks,

HS

Nov 12 '06 #3
ho**********@gmail.com said the following on 11/11/2006 10:09 PM:
Peter Michaux wrote:
>see stopPropogation() and preventDefault() for DOM 2 events. See
cancelBubble and returnValue for the IE event model.

Note that some versions of Safari, I think all versions less than 2.0,
and some other browsers cannot stop the link being followed if you are
using DOM 2 type handlers. preventDefault doesn't work in these
browsers for at least the onclick event handlers. This was a bug and
the workaround is to use the old style event attributes like your first
try.

Now I remember why I hate writing javascript. Thanks,
Javascript is only as difficult as you make it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 12 '06 #4
Randy Webb wrote:
>
Javascript is only as difficult as you make it.
I think an boss or client is completely capable of making it very
difficult for another person.

Nov 12 '06 #5
Peter Michaux said the following on 11/11/2006 11:58 PM:
Randy Webb wrote:
>Javascript is only as difficult as you make it.

I think an boss or client is completely capable of making it very
difficult for another person.
And I disagree with that. No matter what the requirements from a
boss/client, it doesn't change Javascript itself. It is still the same
language.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 12 '06 #6
Randy Webb wrote:
Now I remember why I hate writing javascript. Thanks,

Javascript is only as difficult as you make it.
It's not difficult at all, it's tedious. Rather, it's not the language
I hate, it's the contradictory implementation among browsers that I
hate. The language itself I'm ambivalent towards.

Nov 12 '06 #7
On Sat, 11 Nov 2006 23:12:23 -0500, Randy Webb
<Hi************@aol.comwrote:
>ho**********@gmail.com said the following on 11/11/2006 10:09 PM:
>Peter Michaux wrote:
>>see stopPropogation() and preventDefault() for DOM 2 events. See
cancelBubble and returnValue for the IE event model.

Note that some versions of Safari, I think all versions less than 2.0,
and some other browsers cannot stop the link being followed if you are
using DOM 2 type handlers. preventDefault doesn't work in these
browsers for at least the onclick event handlers. This was a bug and
the workaround is to use the old style event attributes like your first
try.

Now I remember why I hate writing javascript. Thanks,

Javascript is only as difficult as you make it.
Yep, things like using addEventListener rather than onclick make it
difficult...

Jim.
Nov 12 '06 #8
VK
ho**********@gmail.com wrote:
Consider the following (where 'a' is an anchor element with a valid URL
href):

a.onclick=help_mode;
function help_mode() { alert("hi"); return false; }

As expected clicking on the link now results in a "hi" but no change in
location, the link isn't followed because onclick returned false. Now
look at what I thought was the same thing:

a.addEventListener('click', help_mode, false);
function help_mode() { alert("hi"); return false; }
They are *completely* different things using *completely* different
mechanics.
The explanation would take too long, for a quick solution (presuming
that you want to overload all links on the page):

function help_mode() { alert("hi"); return false; }

function loophole(){}

// attach
var len = document.links.length;
for (var i=0; i<len; ++i) {
document.links[i].onclick = help_mode;
}

// detach
var len = document.links.length;
for (var i=0; i<len; ++i) {
document.links[i].onclick = loophole;
// "null bug" workaround for some UA's
}

Nov 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by RobG | last post: by
6 posts views Thread by Cockroach | last post: by
5 posts views Thread by moondaddy | last post: by
7 posts views Thread by extremerep | last post: by
4 posts views Thread by sameergn | last post: by
5 posts views Thread by Stuart Shay | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.