473,241 Members | 1,729 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,241 software developers and data experts.

Firefox differences in event handlers through HTML attributes and Javascript DOM

Recently I've rewritten much of my old Javascript code to use DOM functions
so that enhancements can be attached to documents without needing to alter
the HTML code. I assumed that adding event handlers to document.body would
have the same effect as adding those event handlers through attributes of
the body element, but this turned out not to be the case for key events.
Take this example page (IE-specific code removed):

http://www.safalra.com/hotlinkable/key-events.html

Key events will highlight the appropriate table cells in red for a quarter
of a second. It turns out the the DOM equivalent of adding key event
handlers to the body element is to add them to the document object, not
document.body. If the event handler is added to document.body, key events
are only detected for subelements (try typing in the input box).

Is Firefox' behaviour correct, and if so why?

--
Safalra
http://www.safalra.com/programming/javascript/
Mar 24 '07 #1
2 2069
On Sat, 24 Mar 2007 20:04:13 +0000, Safalra wrote:
Recently I've rewritten much of my old Javascript code to use DOM functions
so that enhancements can be attached to documents without needing to alter
the HTML code. I assumed that adding event handlers to document.body would
have the same effect as adding those event handlers through attributes of
the body element, but this turned out not to be the case for key events.
Take this example page (IE-specific code removed):

http://www.safalra.com/hotlinkable/key-events.html

Key events will highlight the appropriate table cells in red for a quarter
of a second. It turns out the the DOM equivalent of adding key event
handlers to the body element is to add them to the document object, not
document.body. If the event handler is added to document.body, key events
are only detected for subelements (try typing in the input box).

Is Firefox' behaviour correct, and if so why?
For the record, Opera behaves in the same way as IE (except it uses the
proper DOM methods to attach events, of course). This is starting to look
more like a bug in Firefox.

--
Safalra
http://www.safalra.com/programming/javascript/
Mar 29 '07 #2
"Safalra" <us****@safalra.comwrote in message
news:1p*****************************@40tude.net...
On Sat, 24 Mar 2007 20:04:13 +0000, Safalra wrote:
>Recently I've rewritten much of my old Javascript code to use DOM functions
so that enhancements can be attached to documents without needing to alter
the HTML code. I assumed that adding event handlers to document.body would
have the same effect as adding those event handlers through attributes of
the body element, but this turned out not to be the case for key events.
Take this example page (IE-specific code removed):

http://www.safalra.com/hotlinkable/key-events.html

Key events will highlight the appropriate table cells in red for a quarter
of a second. It turns out the the DOM equivalent of adding key event
handlers to the body element is to add them to the document object, not
document.body. If the event handler is added to document.body, key events
are only detected for subelements (try typing in the input box).

Is Firefox' behaviour correct, and if so why?

For the record, Opera behaves in the same way as IE (except it uses the
proper DOM methods to attach events, of course). This is starting to look
more like a bug in Firefox.
Firstly, bear in mind I am not JavaScript expert, so if I happen to skip some fundamental
aspect of your theory, feel free to enlighten me.

The document.body event listeners can only be fired within the context of the
document.body. The input (and the table) is part of the document.body, therefore a key
event could only be fired if it could be executed within the context of document.body, for
example, clicking in an input tag within the document.body and firing key events.

document.body.addEventListener('click', function() { alert('within document.body'); },
false);

Only fires if you click *anywhere* between the top of the page and the input. *Not* below
the input (outside of document.body once you do that).

-Lost
Mar 30 '07 #3

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

Similar topics

1
by: Fred | last post by:
This seems awkward, isn't there a better way to place event handlers in the rest of the code with the logic for everything else like you can with VBScript? <script language="javascript" if...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
3
by: Søren M. Olesen | last post by:
Hi In IE there seem to be a global variable window.event, holding the current event....however this dowsn't work in FireFox... is there a similar way to get the current event?? TIA Søren
4
by: Dave H | last post by:
I originally posted a message "Getting the current cursor position using clientX,clientY" in which I hypothesised that the problem I was having was related to event handler execution order. I am...
9
by: chandramohan.mani | last post by:
Does Event handlers work in netscape. If yes means can anyone please help me. <HTML><SCRIPT LANGUAGE="JScript"> function mouseclick() { alert("I was clicked on " +...
2
by: jdlwright | last post by:
Hi all, I busted a gut getting my design-mode iframe to work properly in FF1 and IE, and just when I thought everything worked nice I discovered that in FF1.5 event handlers in the iframe are no...
3
by: yawnmoth | last post by:
I've seen a few webpages that use the javascript pseudo-protocol with event handlers. eg. <input onkeyup="javascript: ..." /> Correct me if I'm wrong, but isn't onkeyup always supposed to be...
2
by: niks | last post by:
Is there a standard way of traversing the DOM to find all the javascript in a document? As far as I know, the only legal positions for javascript in the DOM is within a <scriptelement or in the...
5
by: Ray | last post by:
Hello JavaScript experts, I've been racking my brains for 2 hours and I still haven't figured out this problem. Basically, I have this page (an IE-only page), with a lot of javascript files...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.