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

How to acheive this ..

Hi there..

I have this piece of code to basically understand the event model in
IE.

<html>
<head>
<title>Events</title>
<script>
function callMe(){
alert(this.nodeName)
};

function setEvents(){
document.attachEvent('onclick', callMe);
document.body.attachEvent('onclick', callMe);
var x = document.all;
if (x){
for(var i = 0; i<x.length; i++){
if (x[i].attachEvent){
x[i].attachEvent('onclick', callMe);
}
};
}
}
</script>
</head>
<body onload="setEvents()">
<h1>Events</h1>
<p > The IE event model is called Event bubbling which
prmarily works from most-specific to least-specific object</p>
<input type="button" value="click me"></input>
</body>
</html>

While a similar flavored code works works 'as intended' in Mozilla, in
IE the event callMe there are some subtle differences.

If you notice, in the method callMe I am trying to figure out which is
the node that triggered the onclick event. In Firefox I get the correct
node name whereas in IE (using 7.0 beta and checked the same in 6.0
also) I get 'undefined'.

The event object also offers no assistance to say which is the object
that is throwing up the event (or probably I am not aware of it). The
closest thing that I could find was event.srcElement which basically
points to the originating object only.

Given this my query is - When assigning methods dynamically, as events,
to a set of objects, is there any mechanism to find out which is the
object that is executing the method?

Thank you.

May 5 '06 #1
5 2549
sr***************@gmail.com said on 05/05/2006 3:42 PM AEST:
Hi there..

I have this piece of code to basically understand the event model in
IE.

<html>
<head>
<title>Events</title>
<script>
Don't forget the required type attribute.

function callMe(){
alert(this.nodeName)
When you use attachEvent in IE, -this- will refer to the window object,
not the element calling the function. Not all the elements you add a
handler to have a nodeName (because of the way you've used
document.all), try:

if (this && this.nodeName){
alert(this.nodeName);
}

};
That semi-colon is redundant, it is treated as an empty statement.


function setEvents(){
document.attachEvent('onclick', callMe);
document.body.attachEvent('onclick', callMe);
var x = document.all;
if (x){
for(var i = 0; i<x.length; i++){
if (x[i].attachEvent){
x[i].attachEvent('onclick', callMe);
If you want to use -this- and make it cross-browser, use:

x[i].onclick = callMe;

}
};
That semi-colon is redundant, it is treated as an empty statement.

}
}
</script>
</head>
<body onload="setEvents()">
<h1>Events</h1>
<p > The IE event model is called Event bubbling which
prmarily works from most-specific to least-specific object</p>
<input type="button" value="click me"></input>
HTML input elements don't have a closing tag, I think you're confusing
it with a button element.

</body>
</html>

While a similar flavored code works works 'as intended' in Mozilla, in
IE the event callMe there are some subtle differences.
Not subtle, totally different in many respects :-).

Read the events stuff at quirksmode:

<URL:http://www.quirksmode.org/js/introevents.html>

If you notice, in the method callMe I am trying to figure out which is
the node that triggered the onclick event. In Firefox I get the correct
node name whereas in IE (using 7.0 beta and checked the same in 6.0
also) I get 'undefined'.
IE's attachEvent is different (broken?), in your code -this- will refer
to the window object.

The event object also offers no assistance to say which is the object
that is throwing up the event (or probably I am not aware of it). The
closest thing that I could find was event.srcElement which basically
points to the originating object only.

Given this my query is - When assigning methods dynamically, as events,
to a set of objects, is there any mechanism to find out which is the
object that is executing the method?


Don't use attachEvent, add the handler directly. If you need to add
multiple handlers, you'll have to use srcElement or some other strategy.

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
May 5 '06 #2
sr***************@gmail.com wrote:
I have this piece of code to basically understand the event
model in IE.

<html>
<head>
<title>Events</title>
<script>
function callMe(){
alert(this.nodeName)
};

function setEvents(){
document.attachEvent('onclick', callMe);
document.body.attachEvent('onclick', callMe);
var x = document.all;
if (x){
for(var i = 0; i<x.length; i++){
if (x[i].attachEvent){
x[i].attachEvent('onclick', callMe);
}
};
}
}
</script> <snip> ... . In Firefox I get the
correct node name whereas in IE (using 7.0 beta and checked
the same in 6.0 also) I get 'undefined'.
The - attachEvent - mechanism in IE does not execute the functions
associated with the Element as methods of the element, so the - this -
keyword will always refer to the global object. The global object does
not have a - nodeName - property.

<snip> Given this my query is - When assigning methods dynamically,
as events, to a set of objects, is there any mechanism to
find out which is the object that is executing the method?


Either assign a reference to the function to the - onclick - property of
the element:-

....
x[1].onclick = callMe;
....

- so that it will be executed as a method of the Element, or use a
closure[1] to associate the element with the a- function:-

function getOnclickReportFnc(element){
return (function(){
alert(element.nodeName);
]);
}

- with:-

....
x[i].attachEvent('onclick', getOnclickReportFnc(x[i]));
....

Richard.

[1] <URL: http://jibbering.com/faq/faq_notes/closures.html > See
particularly the section on IE memory leaks in connection with this.
May 5 '06 #3
Richard Cornford wrote:
<snip>
x[1].onclick = callMe;
...

- so that it will be executed as a method of the Element, or
use a closure[1] to associate the element with a - function:-

function getOnclickReportFnc(element){
return (function(){
alert(element.nodeName);
]); ^
That should be a closing brace (- } -) not a closing square bracket.

Richard.
}

<snip>

Richard.
May 5 '06 #4
Thank you, the method you have suggested works.

May 5 '06 #5
Thank you for your detailed response.

May 5 '06 #6

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

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
28
by: Brent Eamer | last post by:
function SetDefaultDate() { d = new Date(); return d; } ........ <TD align=left> Start Date: </TD> <TD align=left> <SELECT name="batchStartDate" size="1" maxlength="50"...
54
by: richard_quick_uk | last post by:
Hi, If anyone's got the time I'd really appreciate any feedback on the accessibility of this site: http://www.cata.co.uk/_index.a­sp
6
by: bazza | last post by:
Hi, Ive got a web page which I have distilled down to the following simple HTML... <html> <head> <title>Test</title> </head> <body> <table style="width:100%;height:100%"> <tr...
4
by: ghanley | last post by:
Hi Guys, I need some help with a problem related to the looping through a recordset to display on a tree view control. My table has no Primary key - because duplicates are allowed. ID ...
66
by: jacob navia | last post by:
The english word "Initialized" exists. (Cambridge dictionary finds it). The word "Uninitialized" doesn't seem to exist, and no dictionary has it. I am using that word very often in my tutorial of...
5
by: miki | last post by:
Hi all, How can I cast from a string to an object? For example, suppose I have classes as employee, manager, supervisor, director,...I have a user interface that takes a person name, then...
15
roccos
by: roccos | last post by:
Hi Guys, I have developed a php based simple web application which allows users to view articles, comment on articles ,etc The features of the application are: Member Features: • Allows...
6
by: iheartvba | last post by:
Hi I have got the following code from someone else Sub MultiColsToA() Dim rCell As Range Dim lRows As Long Dim lCols As Long Dim lCol As Long Dim ws As Worksheet ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.