attachEvent - wish to provide arguments to calling method

R

Rupe

I wish to add an event to an element, but I wish to add arguments to
the calling method, eg like so:

element.attachEvent('onclick',startDragDrop('arg1',arg2'))

I understand this is not possible. For a non windows browser I have
been successful with the following code:


newA.data = "argumentText"
if(navigator.appName=="Microsoft Internet Explorer"){
//attach events
}
else{
newA.onmouseover = function handler(evt){imageOn(this.data);}
newA.onmouseout = function handler(evt){imageOff(this.data);}
}

by way of a handler, but how could I do this with the IE? the same
code won't work
 
R

RobG

I wish to add an event to an element, but I wish to add arguments to
the calling method, eg like so:

element.attachEvent('onclick',startDragDrop('arg1',arg2'))

I understand this is not possible. For a non windows browser I have
been successful with the following code:

newA.data = "argumentText"
if(navigator.appName=="Microsoft Internet Explorer"){
//attach events
}
else{
newA.onmouseover = function handler(evt){imageOn(this.data);}
newA.onmouseout = function handler(evt){imageOff(this.data);}
}

by way of a handler, but how could I do this with the IE? the same
code won't work

You will find this thread helpful (subject " attachEvent and
arguments"):

<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/92187c8705eb3e79/903e4f22ce34512d?lnk=gst&q=
+attachEvent+and+arguments&rnum=1#903e4f22ce34512d >
 
R

Rupe

my solution actually got it to work by using an intermediary function
and the event object. But I don't think it's ideal

function myEvent(evt){
if(evt.type=="mouseover")
imageOn(evt.srcElement.id);
else
imageOff(evt.srcElement.id);
}

function imageOn(imgName) {
if (document.images) {
if(isIE){
document.all[imgName].src = onImgArray[imgName].src;
}
else
document.images[imgName].src = onImgArray[imgName].src;
}
}

two functions to run what should simply be one, is pretty ordinary,
but it got the job done.
 
R

RobG

Rupe said:
my solution actually got it to work by using an intermediary function
and the event object. But I don't think it's ideal

Far from it.
function myEvent(evt){
if(evt.type=="mouseover")
imageOn(evt.srcElement.id);
else
imageOff(evt.srcElement.id);
}

function imageOn(imgName) {
if (document.images) {
if(isIE){

Browser detection went out of fashion a long time ago, use feature
detection or a different scheme to link the elements.

document.all[imgName].src = onImgArray[imgName].src;
}
else
document.images[imgName].src = onImgArray[imgName].src;

There appear to be some braces missing here.

}
}

two functions to run what should simply be one, is pretty ordinary,
but it got the job done.

It seems you have either an element with an ID that is the same as some
other element's name property or you are using an element reference to
get its id to get a reference back to the same element. Both are poor
design decisions - the first is worse than the second.

You should be aware that evt.srcElement will not work in browsers that
do not support IE's proprietary event model - the W3C equivalent is
event.target.

Also, the element referenced by event.target may be a text node and
therefore may not have an ID attribute, or at least not the one you are
after.

If you explain a bit more about what you are trying to do, you'll get
better help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top