Empty event object after iframe onload in Firefox

E

Elias

Here's a genuinely unfamiliar sensation: something that works exactly
as expected in Internet Explorer, doesn't work in Firefox at all.

Consider the following code, placed in the onload attribute of the
body element;

document.getElementById('theIframe').addEventListener('load',function
(e)
{
alert(e.toSource());
},false)
document.getElementById('theIframe').src = 'test2.html';

....and an iframe in the body:

<iframe id="theIframe" name="theIframe"></iframe>

While IE (with the required modifications, such as calling attachEvent
and skipping toSource) returns a perfectly normal event object
complete with srcElement (containing the iframe) - Firefox returns an
object that is completely devoid of properties!
This appears to hold true not only for the load event, but at least
click and mouseover too.
At first I was sceptical towards attaching onload to an iframe, but W3
Schools did specify this event valid for the iframe element. Haven't
been able to find any information on the subject at w3.org though.


Anyway, I have found a way around having to use the event object, but
I'm still wondering - has anyone else noticed this, and is there any
way to do this in FF?
 
T

Thomas 'PointedEars' Lahn

Elias said:
Here's a genuinely unfamiliar sensation: something that works exactly
as expected in Internet Explorer, doesn't work in Firefox at all.

BAD. Broken as designed.
Consider the following code, placed in the onload attribute of the
body element;

document.getElementById('theIframe').addEventListener('load',function
(e)
{
alert(e.toSource());
},false)
document.getElementById('theIframe').src = 'test2.html';

You should refrain from repeatedly using such reference worms. Do runtime
feature tests, if successful assign the object reference to a variable,
and reuse the variable value instead. For example, you can't be sure that
the call of gEBI() returns an object reference, much less that the referred
object has an addEventListener() method.

See http://PointedEars.de/scripts/test/whatami pp.
...and an iframe in the body:

<iframe id="theIframe" name="theIframe"></iframe>

While IE (with the required modifications, such as calling attachEvent
and skipping toSource) returns a perfectly normal event object
complete with srcElement (containing the iframe) - Firefox returns an
object that is completely devoid of properties!

You are mistaken. The properties simply do not show up in the return value
of e.toSource(). (That should not come as a surprise as `e' is a reference
to a host object.) However, the object referred to by `e' has several
enumerable properties:

o.addEventListener(
"click",
function(e) {
var a = [];
for (var p in e) a.push(p);
window.alert(a.join(", "));
},
false);

It may have also non-enumerable properties that can't be determined unless
you know the name of the respective property. (RTFM)
This appears to hold true not only for the load event, but at least
click and mouseover too.

It does not.
At first I was sceptical towards attaching onload to an iframe, but W3
Schools did specify this event valid for the iframe element.

Despite its name, due to enough examples of nonsensical content or at least
wildly inaccurate one from that source, w3schools.com can be safely
recommended against as Web development reference.
Haven't been able to find any information on the subject at w3.org though.

http://www.w3.org/TR/DOM-Level-2-Events/

http://www.w3.org/TR/DOM-Level-3-Events/ (WD, but partially implemented by
the Gecko DOM)
Anyway, I have found a way around having to use the event object, but
I'm still wondering - has anyone else noticed this, and is there any
way to do this in FF?

There is the way documented in the Specification(s) and at MDC.

http://developer.mozilla.org/en/docs/DOM:element.addEventListener


HTH

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top