473,473 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1557

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

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
6
by: Cockroach | last post by:
Hello, I have a problem where the onClick of a table row will activates a window.location event, and inside a cell in that row, an image onClick event shows/hides a div. The problem is that...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
5
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
7
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The...
4
by: sameergn | last post by:
Hi, I have an image in my HTML form which has onclick() handler. There is also a submit button and a text box. Whenever text box has focus and user presses enter, the onclick() event of...
5
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.