469,951 Members | 2,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

using OnLoad

PJ6
Say I have serveral controls, all of which need to emit clientside script to
execute on page load. They can't emit to OnLoad = <functionname> because
then only one of the scripts will get executed. Is there a way around this?

Paul
Sep 13 '05 #1
4 4327


PJ6 wrote:
Say I have serveral controls, all of which need to emit clientside script to
execute on page load. They can't emit to OnLoad = <functionname> because
then only one of the scripts will get executed. Is there a way around this?


Yes, but how easy you solve that depends on the browsers you target.
If you only look at W3C DOM Level 2 compatible ones like Mozilla and at
IE 5.5 and later on Win you have
if (typeof window.addEventListener != 'undefined') {
window.addEventListener(
'load',
yourFunction,
false
);
}
else if (typeof window.attachEvent != 'undefined') {
window.attachEvent(
'onload',
yourFunction
);
}
to be able to attach as meny different load handlers as needed from
different locations/scripts or in your case "several controls".

For older browsers not supporting addEventListener or attachEvent you
would then need to build your own mechanism e.g. set up and array
var loadHandlers = [];
each time a control needs to set up a handler you do e.g.
loadHandlers[loadHandlers.length] = yourFunction;
and then you need to make sure you have the following too
window.onload = function (evt) {
for (var i = 0; i < loadHandlers.length; i++) {
loadHandlers[i]();
}
};

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 13 '05 #2
"PJ6" <no****@nowhere.net> wrote in message
news:xxAVe.10322$c27.3429@trndny01...
Say I have serveral controls, all of which need to emit clientside script
to execute on page load. They can't emit to OnLoad = <functionname>
because then only one of the scripts will get executed. Is there a way
around this?


Is this what you meant?

<body onLoad="function1();function2();function3()">
Sep 13 '05 #3
PJ6
Hmmm. That looks like a good way to do it, too. I'd just have to access the
body html element and concatenate extra function names to the OnLoad
attribute, right? With a little work I could even code in some precedence,
like a z-index, only for methods.

Paul

"Danny@Kendal" <da***@STOPSPAMghpkendal.co.uk> wrote in message
news:A7*****************************************@e clipse.net.uk...
"PJ6" <no****@nowhere.net> wrote in message
news:xxAVe.10322$c27.3429@trndny01...
Say I have serveral controls, all of which need to emit clientside script
to execute on page load. They can't emit to OnLoad = <functionname>
because then only one of the scripts will get executed. Is there a way
around this?


Is this what you meant?

<body onLoad="function1();function2();function3()">

Sep 13 '05 #4
PJ6 wrote:

Top-posting is not liked here.
Hmmm. That looks like a good way to do it, too. I'd just have to access the
body html element and concatenate extra function names to the OnLoad
attribute, right? With a little work I could even code in some precedence,
like a z-index, only for methods.


What do you mean by access the onload attribute? If you mean doing it
on the server and changing the value of the body's onload attribute in
its HTML source, then you're OK. But you can't access the body's onload
attribute using something like:

alert( body.onload );

The value of the body's onload attribute is added to the window object,
you'll find your functions there:

<head><title>onload play</title>
<script type="text/javascript">
function sayHi(){}
</script>
</head>
<body onload="sayHi();">
<input type="button" value="Show body attributes" onclick="
var prop, i=0;
var txt = ['<b>Body attributes:</b>'];
for ( prop in document.body.attributes ) {
txt.push( i++ + ': ' + prop + ' : '
+ document.body.attributes[prop]);
}
if (window.onload){
txt.push('<br><b>window.onload:</b>',window.onload.toString());
}
document.getElementById('msg').innerHTML = txt.join('<br>');
">

<div id="msg"></div>
</body>
[...]
--
Rob
Sep 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Pai | last post: by
4 posts views Thread by David Virgil Hobbs | last post: by
10 posts views Thread by berg | last post: by
3 posts views Thread by Vik Rubenfeld | last post: by
20 posts views Thread by Mark Anderson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.