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

Home Posts Topics Members FAQ

addEventHandler / addEvent conflict


All,

I have a generic script to handle events. (NN/IE only)

function addEvent(elementObject, eventName, functionObject)
{
if(document.addEventListener)
elementObject.addEventListener(eventName, functionObject, false);
else
if(document.attachEvent)
elementObject.attachEvent("on" + eventName, functionObject);
}

Now, what I wanted to do was pass a parameter to the functionObject, but
this is apparently not possible.
How can I determine what object caused the event in the functionObj?
FWIU, the "this" object is fine in NN, but in IE, it refers to window.

addEvent(document.getElementById("myE"),"click",my Function);
function myFunction()
{
// I need a reference to document.getElementById("myE") or whatever
object triggered this function to be called
}

If someone knows a way to pass a parameter, that is preferable. :)

TIA
--
--
~kaeli~
Local Area Network in Australia:... the LAN down under.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #1
4 10293


kaeli wrote:
I have a generic script to handle events. (NN/IE only)

function addEvent(elementObject, eventName, functionObject)
{
if(document.addEventListener)
elementObject.addEventListener(eventName, functionObject, false);
else
if(document.attachEvent)
elementObject.attachEvent("on" + eventName, functionObject);
}

Now, what I wanted to do was pass a parameter to the functionObject, but
this is apparently not possible.
How can I determine what object caused the event in the functionObj?
FWIU, the "this" object is fine in NN, but in IE, it refers to window.


With IE the element the event is fired on is
window.event.srcElement
with a DOM compliant browser your functionObject should have a parameter say
function functionObject (evt) {...}
and then you can access
evt.target
as the node the event was fired on.
As you are asking for the this object, that would be
evt.currentTarget

For IE you will need a closure e.g.

function addEvent(elementObject, eventName, functionObject)
{
if(document.addEventListener)
elementObject.addEventListener(eventName,
function (evt) {
functionObject(elementObject, evt)
}, false);
else
if(document.attachEvent)
elementObject.attachEvent("on" + eventName,
function () {
functionObject(elementObject);
}
)
}
with
function myFunction (element, evt) {
// access element here if needed
}
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
In article <40********@olaf.komtel.net>, ma*******@yahoo.de enlightened
us with...


function addEvent(elementObject, eventName, functionObject)
{
if(document.addEventListener)
elementObject.addEventListener(eventName,
function (evt) {
functionObject(elementObject, evt)
}, false);
else
if(document.attachEvent)
elementObject.attachEvent("on" + eventName,
function () {
functionObject(elementObject);
}
)
}
with
function myFunction (element, evt) {
// access element here if needed
}


Worked great, thanks!

For archival purposes, in the html, I named the elements with a number
at the end, then in myFunction used
element.name.substring(element.name.length-1)
to retrieve the number.
The purpose: correspondingly named textboxes and selects were being
synchronized and I wanted the function to be able to be used for all of
them.

textbox1, select1
textbox2, select2
etc
The function could then tell which text element to modify when a
particular select element was changed. Previously a reference to both
objects had been passed.

--
--
~kaeli~
Dancing cheek-to-cheek is really a form of floor play.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #3
kaeli <ti******@NOSPAM.comcast.net> writes:
I have a generic script to handle events. (NN/IE only)
NN? Not Netscape 4 for sure, but it works in all DOM 2 compliant browsers,
which include Opera 7.
function addEvent(elementObject, eventName, functionObject)
{
if(document.addEventListener)
elementObject.addEventListener(eventName, functionObject, false);
else
if(document.attachEvent)
elementObject.attachEvent("on" + eventName, functionObject);
}
Yes, this will work in most cases.
Now, what I wanted to do was pass a parameter to the functionObject, but
this is apparently not possible.
It already gets one argument, the event. There is no direct way to store
another value (or more) with it.
How can I determine what object caused the event in the functionObj?
Normally (if assigned to the "onclick" attribute or with
addEventListener), "this" would refer to the element, because the
function is treated as a method of the element object.
FWIU, the "this" object is fine in NN, but in IE, it refers to window.
..... but not with attachEvent.
addEvent(document.getElementById("myE"),"click",my Function);
function myFunction()
{
// I need a reference to document.getElementById("myE") or whatever
object triggered this function to be called
} If someone knows a way to pass a parameter, that is preferable. :)


Try this general function instead:
---
function addEvent(elementObject, eventName, functionObject /* , ... */)
{
var args = Array.prototype.slice.call(arguments,3);
function wrapper(event) {
return functionObject.apply(elementObject,[event].concat(args));
}
if (document.addEventListener) {
elementObject.addEventListener(eventName, wrapper, false);
} else if (document.attachEvent) {
elementObject.attachEvent("on" + eventName, wrapper);
}
}
---
It has the disadvantage of not allowing detachEvent or removeEventListener
to remove the function. For that you need something more complicated, but
doable.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
In article <hd**********@hotpop.com>, lr*@hotpop.com enlightened us
with...
kaeli <ti******@NOSPAM.comcast.net> writes:
I have a generic script to handle events. (NN/IE only)
NN? Not Netscape 4 for sure, but it works in all DOM 2 compliant browsers,
which include Opera 7.


Sorry, clarification: NN6+/IE5+ only are the only ones tested and the
only ones I care about. Just didn't want people telling me about all the
browsers it didn't work in. Intranet. *g*
It has the disadvantage of not allowing detachEvent or removeEventListener
to remove the function.


I don't need to and Martin's function worked great. :)
This was a workaround so I could have my taglibraries (JSP) generate my
form elements (dynamically made as normal or readonly based on user's
permissions). The problem was that I didn't want to attach javascript
from the tag libraries, as the elements aren't always scripted the same
way and I wanted the script to be modifiable and removable with no
modification to the taglib. So I needed the elements and the script
separate.

Thanks!

--
--
~kaeli~
If the funeral procession is at night, do folks drive with
their lights off?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #5

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

Similar topics

0
2343
by: Adam | last post by:
Hi I had Ruby 1.6.8-8 for Windows installed on my WinXP Pro machine prior to installing Python 2.3. After installing Python 2.3, I tried to <----- screen output of python interactive command...
0
317
by: Zoury | last post by:
Hi folks ! :O) Let's say I have the following EventHandler defined in a class : '*** Option Explicit On Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As MyEventArgs) ...
4
2357
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
0
1100
by: Archie14 | last post by:
I have C++ code that has to use external assembly. This assembly is a component and it has one event delegate. Let's say this component has following code: namespace MyNamespace.MyModel { public...
4
3310
RMWChaos
by: RMWChaos | last post by:
Darnit all, I expect the code I steal from others to work! =D Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the...
1
1832
RMWChaos
by: RMWChaos | last post by:
I grabbed this "Rock Solid addEvent" code from this site, which is based on Mark Wubben's event-cache code. (These links for reference only.) I am having two problems with it, and the webmaster is...
14
3510
lotus18
by: lotus18 | last post by:
Hello World I have a problem in detecting the conflict schedule (Day and Time). Day 1. M 2. T 3. W 4. TH 5. F
14
6864
lotus18
by: lotus18 | last post by:
Hello all I have these records on my Day Table for my complete database table please click here 1. M 2. T 3. W 4. TH 5. F 6. S
53
2858
by: Aaron Gray | last post by:
I jokingly say this is the late entry :) Okay I have read all the event entry comments from John's Resig's AddEvent comepition blog :- http://ejohn.org/projects/flexible-javascript-events/ ...
0
7041
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
6908
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
7084
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...
1
6739
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
5337
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,...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
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...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.