473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

onload handler order of firing

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?

Cheers
Adam

Jan 23 '06 #1
4 3751
Adam Ratcliffe wrote:
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.
--
Rob
Jan 23 '06 #2
On 2006-01-23, Adam Ratcliffe <ad************@gmail.com> wrote:
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?


no. not even a guarantee that both will fire.

--

Bye.
Jasen
Jan 23 '06 #3
Hi Rob

Your quite correct about there being no guarantee of the order in which
the event handlers will be called. Yesterday I had the opportunity to
test on a windows machine and the body element's onload handler was
called first (the opposite to FF).

The reason that the onload handler defined in the script tag wasn't
invoked in your example was because attachEvent() requires that the
event type is prefixed with 'on'.

Cheers
Adam

Jan 24 '06 #4
VK

Adam Ratcliffe wrote:
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?


There were some test made in this group a couple of month ago. It was
shown that addEventListener sequence is preserved, but attachEvent is
randomized (MSDN actually clearly states that). Also these test have
been made for addEventListener/attachEvent only (w/o intrinsic
handlers) and for Firefox / IE only. So on the global run no - the
sequence is not guaranteed. As suggested, if the sequence is crucial
for your case, you have to keep your own pool manually.
....
function init() {
for (var i=0; i<init.stack.length; i++) {
var foo = init.stack[i]();
}
} init.stack = new Array();
....
if (window.onload != null) {
init.stack.push(window.onload);
}
window.onload = init;
init.stack.push(extraFunction1);
init.stack.push(extraFunction2);
.... etc.

Jan 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bender | last post by:
Hi, I am wanting to capture an onmousedown event without firing the body tags onload event. Also, if anyone could explain why this happens that would be excellent. I can't see how an...
4
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello');...
1
by: Adam Ratcliffe | last post by:
I'm trying to come up with a solution for detecting when an image, loaded by a script, has completely loaded. The Image.onload event is fired after the image has loaded in Firefox but before...
3
by: usenet | last post by:
Hi All, I have some initialization to be done at page load time, which changes the text of some anchors so that they are consistent with the query string (these anchors are used as criteria...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: ajfish | last post by:
Hi, I have a web form with smart navigation turned on. the html has an onload event which calls a javascript function and the contents of this function are dynamically generated on the server...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
1
by: sklett | last post by:
I've got a strange situation here. I have a databound DataGridView that also has un-bound columns. When the view loads, I want to update the values of the unbound columns. If I attempt to modify...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.