onload without <body> tag?

B

bmgz

I need to execute a JavaScript function "onload". The only problem is I
don not have access to the <body> tag as it is a part of the standard
page-header include (a separate file). How could I have certain pages
execute my function() onLoad?

The function basically just sets the original values of fields so that I
can determine if a field has been changed or not, which aleviates unnec.
sql update on the backend..
 
M

Martin Honnen

bmgz said:
I need to execute a JavaScript function "onload". The only problem is I
don not have access to the <body> tag as it is a part of the standard
page-header include (a separate file). How could I have certain pages
execute my function() onLoad?

Put
window.onload = function (evt) {
yourLoadHandler();
};
in a script (preferably included in the head section).

In newer browsers you have also attach multiple event listeners e.g. for
Mozilla or other DOM Level 2 Events compliant browsers you can do
if (typeof window.addEventListener != 'undefined') {
window.addEventListener(
'load',
function (evt) {
yourLoadHandler();
},
false
);
}
and in IE 5.5 and later you can do
else if (typeof window.attachEvent != 'undefined') {
window.attachEvent(
'onload',
function () {
yourLoadHandler();
}
);
}
That way multiple independent scripts can have load handlers added.
 
R

RobG

bmgz said:
I need to execute a JavaScript function "onload". The only problem is I
don not have access to the <body> tag as it is a part of the standard
page-header include (a separate file). How could I have certain pages
execute my function() onLoad?

The function basically just sets the original values of fields so that I
can determine if a field has been changed or not, which aleviates unnec.
sql update on the backend..

function aFunction() {
// do stuff
}
window.onload = aFunction;
 
M

Mick White

bmgz said:
I need to execute a JavaScript function "onload". The only problem is I
don not have access to the <body> tag as it is a part of the standard
page-header include (a separate file). How could I have certain pages
execute my function() onLoad?

The function basically just sets the original values of fields so that I
can determine if a field has been changed or not, which aleviates unnec.
sql update on the backend..

window.onload=function(){//do stuff here}

But the body tag must not have an "onload" handler.
Mick
 
B

bmgz

RobG said:
function aFunction() {
// do stuff
}
window.onload = aFunction;

Thanks, but I had already tried this before posting. I have all my
JavaScript in a seperate .js file, could this be the problem?

onload is triggering before the form has even loaded, because when I put
the script after the form, it works.
 
R

RobG

bmgz said:
Thanks, but I had already tried this before posting. I have all my
JavaScript in a seperate .js file, could this be the problem?

onload is triggering before the form has even loaded, because when I put
the script after the form, it works.

Do not include the brackets '()' after the function name:

window.onload = aFunction(); // function will run immediately

That will cause the function will run immediately rather than when the
window has finished loading. If you need to pass parameters, use one
of the other suggested methods, e.g.

window.onload = function() {
aFunction(parm1, parm2);
};

See the other posts.
 
L

Lee

Tony said:
Wouldn't it work just to put

<script type='text/javascript'>
// script to execute
</script>

in the HTML body? My understanding is that any script placed in the body
will execute when the page is loaded.

Actually, it's executed as the page is loading, which isn't quite the
same thing.
 
M

Mick White

Tony said:
Martin Honnen wrote: [...]
Put
window.onload = function (evt) {
yourLoadHandler();
};
in a script (preferably included in the head section).


It would appear from what he said that he can't put anything in the header,
either.

Wouldn't it work just to put

<script type='text/javascript'>
// script to execute
</script>

<script type='text/javascript'>
window.onload = function () {
yourLoadHandler();
}
</script>

You may place this anywhere on your page.
Mick
 

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
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top