By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

onload without <body> tag?

P: n/a
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..
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a


bmgz wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
bmgz wrote:
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;
--
Rob
Jul 23 '05 #3

P: n/a
bmgz wrote:
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
Jul 23 '05 #4

P: n/a
RobG wrote:
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.
Jul 23 '05 #5

P: n/a
bmgz wrote:
RobG wrote:
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.


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.

--
Rob
Jul 23 '05 #6

P: n/a
RobG wrote:
Do not include the brackets '()' after the function name:


thanks for that.
Jul 23 '05 #7

P: n/a
Martin Honnen wrote:
bmgz wrote:
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).


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>

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

--
Tony Garcia
Web Right! Development
to*****@webrightdevelopment.SPAM.com
Jul 23 '05 #8

P: n/a
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.

Jul 23 '05 #9

P: n/a
Tony wrote:
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
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.