VK said:
How can I be notified when the document load is complet in JavaScript?
I am referring to the whold document load is complete, mean all
images/external files/frame/iframes have been loaded.
I think there is a window.addEventListener() function but I am not sure
which event to listen to.
[...]
1. Grey-old way:
Nonsense. It is the standards compliant way and the more recent one:
<body onload="myFunction()">
Note that this is a real function call so parenthesis are obligatory;
also you can provide some arguments to your function:
<body onload="myFunction('Hello world!')">
2. Up to date
Nonsense. It is the other way around. `window.onload' has been there in
AOMs before HTML 4.01 where the `onload' attribute and other event handler
attributes were introduced.
Which does not make it in any way more correct. Especially when such a
statement comes from you who has proven not to understand what he is doing.
<script type="text/javascript">
finction myFunction() {
function ...
// ...
}
window.onload = myFunction;
This is the proprietary approach, dating back to IE3/NN3. It also does not
ensure that the document was loaded; it ensures only that the window was
loaded, iff the object referred to by `window' supports an `onload' event
handler property.
</script>
Note that this is a function *reference* so no parenthesis are allowed;
Nonsense. Parantheses, i.e. the Call Operator, are very well allowed here,
but that changed the meaning of that AssignmentExpression as the return
value of the called function would be used then. It is entirely possible
that a function call may return a Function reference.
also you cannot directly provide arguments to your function.
True, however you can indirectly:
window.onload = function(e)
{
myFunction('foo');
}
Nonsense. The first branch is the standards compliant approach to
implement the feature the standards compliant attribute of the `body'
element facilitates, using scripting only:
<script type="text/javascript">
finction myFunction() {
It is still `function'.
// ...
}
if (window.addEventListener) {
// using dis-on'ed event name:
window.addEventListener('load', myFunction, false);
addEventListener() is not a method of Window objects, but of EventTarget
objects, according to the W3C DOM Level 2 Events Specification. The
`load' event there does not apply to Window objects in HTML context,
but to the document, frameset and object element, hence HTMLDocument,
HTMLFramesetElement and HTMLObjectElement objects, where the first one
is usually implemented referred to by the `document' property.
<URL:
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget>
<URL:
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>
And of course the feature detection used here is flawed. So it should be
something more like
function isMethodType(s)
{
return (s == "function" || s == "object");
}
var d;
if (typeof window != "undefined"
&& typeof (d = window.document) != "undefined"
&& isMethodType(typeof d.addEventListener))
{
d.addEventListener('load', myFunction, false);
}
}
else if (window.attachEvent) {
// using on'ed event name:
The main difference is that the former, if it was used correctly, is the
standards compliant approach while attachEvent() is the IE-proprietary one:
window.attachEvent('onload', myFunction);
}
else {
// some old crap.
// either use option 2 from above
// or
// alert('Get yourselve a normal browser!');
More of the usual VK nonsense.
}
</script>
Also please not that "window.load" event doesn't mean that every single
bit of page is loaded. It can be enbedded "Star Wars" movie on the
page, so the actual loading will be finished an hour or two later.
"window.load" means that DOM structure is fully parsed
No, that is exactly what it does not mean.
so you can reliably address any DOM element on the page by id / name,
change src and other attributes and so on - briefly you are able to script
the loaded page.
No, you cannot and you are not reliably then.
Your advice is considered harmful rightfully. Stop pretending
you had a minimum clue about the matters discussed here.
PointedEars