adding event with parameters dynamically?

Z

Zwerfkat

In my HTML code is somewhere listed:

<div id=mydif onmouseover="do_something(this,3,2,1);">test</div>

Is there a way to add/change this mouseover event dynamically, including its parameters?

Because it looks like:

obj= getElementbyID("mydif");
obj.onmouseover = do_something;
or:
obj.attachEvent("onmouseover",do_something);


does not allow me to add parameters to the event, or?
 
M

Martin Honnen

Zwerfkat said:
In my HTML code is somewhere listed:

<div id=mydif onmouseover="do_something(this,3,2,1);">test</div>

Is there a way to add/change this mouseover event dynamically, including its parameters?

Because it looks like:

obj= getElementbyID("mydif");
obj.onmouseover = do_something;

does not allow me to add parameters to the event, or?

Well you might want to check what
document.getElementById('mydif').onmouseover
shows for your HTML snippet above, then you will find e.g.
function (event) { do_something(this,3,2,1); }
that way, if you want to construct the same with script then it should
be clear to use e.g.
document.getElementById('mydif').onmouseover = function (evt) {
do_something(this,3,2,1);
};
which is simply a function expression where the body of the function has
the do_something call.
 
T

Thomas 'PointedEars' Lahn

Zwerfkat said:
In my HTML code is somewhere listed:

<div id=mydif onmouseover="do_something(this,3,2,1);">test</div>

Is there a way to add/change this mouseover event dynamically, including its parameters?

Because it looks like:

obj= getElementbyID("mydif");
obj.onmouseover = do_something;
or:
obj.attachEvent("onmouseover",do_something);

does not allow me to add parameters to the event, or?

Neither one is equivalent to the HTML code. You have to understand first
that there are events, event handlers and event listeners. The event here
is `mouseover'. It has a corresponding intrinsic event handler attribute,
`onmouseover'. By setting that attribute, an event listener for the
`mouseover' event is added to the corresponding DOM object that has the
attribute value as its code. It looks like this:

function()
{
do_something(this, 3, 2, 1);
}

But the signature of the event listener, i.e. the arguments it is being
passed, is built into the UA's DOM and cannot be modified. In standards
compliant DOMs, event listeners implement the EventListener interface,
where the Event object is passed as first and only argument:

http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventListener

In the MSHTML DOM, no argument is passed; the reference to the Event object
has to be obtained through window.event:

http://msdn2.microsoft.com/en-us/library/ms535863.aspx

obj.attachEvent() works differently. The same as the standards compliant
obj.addEventListener(), this IE-proprietary method *adds* an event listener
to a DOM object. In contrast to obj.addEventListener() where the last
listener added will be the last one that is executed on event, the order of
listeners is not guaranteed with obj.attachEvent().

http://msdn2.microsoft.com/en-us/library/ms536343.aspx
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget-addEventListener

So obj.attachEvent() and obj.addEventListener() *alone* are the wrong
methods to call when you want to replace the event listener. However,
you can try removing the previously assigned event listener first and
then add another one:

obj.detachEvent("onmouseover", obj.onmouseover);
obj.attachEvent("onmouseover", function() { window.alert(42); });

or

obj.removeEventListener(
"mouseover", obj.onmouseover, false);
obj.addEventListener(
"mouseover", function() { window.alert(42); }, false);

That would still rely on the proprietary `onmouseover' property to yield a
reference to the event listener, and I know of no way to avoid that. The
only alternative would be to go proprietary completely and assign to the
property itself:

obj.onmouseover = function(e)
{
window.alert(42);
};

A wrapper method could facilitate this resulting in only one direct call.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 9/14/2007 11:01 AM:

Does that imply that there is another type of MS DOM? Maybe an MSXML DOM
or something similar?

AIUI, "MSHTML" is the common name for the combination of the Trident/Tasman
layout engine with the Microsoft Script Engine, the user agent of Microsoft
Internet Explorer (IE), HTAs like the Windows NT 5.x Control Panel,
Microsoft HTML Help, and other IE-based applications (primarily other Web
browsers such as NeoPlanet etc.)

The MSHTML DOM is the DOM supported by this UA for HTML documents and XHTML
documents served as text/html; it is covered by the subsection "HTML and
DHTML Reference" of the MSDN Library:

http://msdn2.microsoft.com/en-us/library/ms533050.aspx

Note that Microsoft still calls this "Dynamic HTML (DHTML) Object Model
Reference"; that is probably due to historic reasons: the term "DOM" for
what was then understood as "DHTML" was coined later.

There is also an MSXML DOM that is used primarily for processing XML
content. It provides for example the recognized IXMLHTTPRequest
interface, and is covered by another subsection of the MSDN Library:

http://msdn2.microsoft.com/en-us/library/ms759148.aspx
If there isn't, then it is simply an MSIE DOM.

Your logic is flawed.
Speaking of proprietary properties (which you like to point out and the
inherent error-prone nature of them), have you found an answer to my
"document" question whereby I wanted to know what you propose people use
instead of the "proprietary and error-prone" document object?

Sorry, I made it a rule for myself not to answer questions that are ripped
out of their context (in a childish attempt to invalidate a proposed
solution by ripping the corresponding quotation out of context.)


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 9/15/2007 5:20 AM:
Sorry, I made it a rule for myself not to answer questions that are ripped
out of their context (in a childish attempt to invalidate a proposed
solution by ripping the corresponding quotation out of context.)

A "childish attempt"? I have asked you, more than once, what you would
suggest people use instead of something that *YOU* term as "proprietary
and error-prone" and you refuse to answer the question. Probably because
you have no answer. [...]

q.e.d.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top