using OnLoad

P

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
 
M

Martin Honnen

PJ6 said:
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();
}
};
 
D

Danny@Kendal

PJ6 said:
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()">
 
P

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
 
R

RobG

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>


[...]
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top