Adam said:
I have 2 onload handlers on a web page. One is set programatically in
an included script and the other declared on the document's body
element.
In practice the programatically set onload handler always fires first
in Firefox (the desired behaviour).
Do I have any guarantee that this ordering will be the same across
other browsers?
That seems a rather risky dependency, even if it is 'works' in practice,
that is easily proved to fail in at least one case (see below).
If the script element is in the header, the browser should parse the
content before getting to the body tag (the defer attribute can suggest
that it doesn't, but I'll guess you're not using that). But I'm not
sure that the onload events will be attached in the sequence you require
with any certainty.
A much safer method would be to have your own init() function that you
can add functions to so that you can guarantee that they run in the
right order.
Consider the following simple example:
<head>
<title>onload test</title>
<script type="text/javascript">
function hiScript() {
alert('hi, script 1');
}
if (window.addEventListener) {
window.addEventListener("load", hiScript, false);
} else {
window.attachEvent('load',hiScript);
}
</script>
</head>
<body onload="alert('hi, body');">
</body>
The alerts appear in the right order in Firefox (script then body), but
in IE I get only the onload attached in the body tag.