What code cause IE to set the `toElement` property?

G

Garrett Smith

I want to set the toElement property on a custom event, however it
cannot be done automatically. If what I read is correct, the reason for
this is that the property is a reference.

Here is a demonstration of the problem.

<html>
<head>
<title>blah</title>
</head>
<body>
<script type="text/jscript">
document.body.onmouseout = function () {
var from = event.fromElement;
from = from && from.nodeName;
alert("fromElement: " + from);
};
document.body.fireEvent("onmouseout" /*,
{fromElement: document.documentElement}*/);</script>
</body>
</html>

I have noticed that if the event is generated by the user, then the
toElement property is set. I have also noticed that if the argument to
createEventObject is an event object with a toElement property, that the
toElement reference is used.

The problem is that the toElement property seem only to be set from a
user-generated event.

Is there any code that will cause IE to set the toElement property?

The reason I want this to work is to test my "getRelatedTarget"
function. I believe the function works (it seems to work as I am using
it), but the unit tests are failing because in IE, the `toElement`
property is null, and not the object that the test is expecting it to be.
 
T

Thomas 'PointedEars' Lahn

Garrett said:
I want to set the toElement property on a custom event, however it
cannot be done automatically. If what I read is correct, the reason for
this is that the property is a reference.
Possible.

Here is a demonstration of the problem.

Your example source code does not relate to what you are describing here.
<html>
<head>
<title>blah</title>
</head>
<body>
<script type="text/jscript">
document.body.onmouseout = function () {
var from = event.fromElement;
from = from && from.nodeName;
alert("fromElement: " + from);
};
document.body.fireEvent("onmouseout" /*,
{fromElement: document.documentElement}*/);</script>

The error message I am getting (80004002) in IE 6.0.2800.1106 when using
that second argument and the documentation for fireEvent() indicate that
fireEvent() cannot be used that way. You can use it for event delegation,
but not creation.
</body>
</html>

I have noticed that if the event is generated by the user, then the
toElement property is set. I have also noticed that if the argument to
createEventObject is an event object with a toElement property, that
the toElement reference is used.

But you are not using createEventObject() here. However, if you use it you
can see that `toElement' is special:

document.body.onmouseover = function () {
/* null */
window.alert(window.event.toElement);
};

var e = document.createEventObject();
e.toElement = document.body;

/* null */
window.alert(e.toElement);

document.body.fireEvent("onmouseover", e);

However:

document.body.onmouseover = function () {
/* 2 */
window.alert(window.event.button);
};

var e = document.createEventObject();
e.button = 2;

/* 2 */
window.alert(e.button);

document.body.fireEvent("onmouseover", e);
The problem is that the toElement property seem only to be set from a
user-generated event.

That appears to be a correct observation.
Is there any code that will cause IE to set the toElement property?

I do not think so; this is very likely due an MSHTML bug as other
properties can be set that way. Meaning that the documentation is correct
that the property is read/write sometimes, but it neglects to mention that
the property then has a setter that still prevents changes to its value.


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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top