473,406 Members | 2,849 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,406 software developers and data experts.

onload without <body> tag?

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
9 19104


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
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
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
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
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
RobG wrote:
Do not include the brackets '()' after the function name:


thanks for that.
Jul 23 '05 #7
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrés Giraldo | last post by:
Hi! It's possible to register some script between the <head> and </head> actually I'm Using RegisterStartupScript and RegisterClientBlockScript, but it works on the body By the way... how...
2
by: Starry Gordon | last post by:
I've been running some small test programs which seem to indicate something noticed in a larger script, that a function called from onLoad() in the <body> tag will not succeed in creating a window...
2
by: ryan.mclean | last post by:
Hello everyone. Hope ya'll had a nice New Year. Anyway, my question is why won't this work? I must be doing something dumb . . . here is the code: in the body tag, I have this code (just to...
1
by: Dung Ping | last post by:
For instance, one set is: <body onload="blinking_header()" onunload="stoptimer()"> Another set is: <body onload="writemsg()" onunload="stoptimer()"> They represent two functions. How to...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
5
by: SteveS | last post by:
Hello, I'm having trouble figuring out how to dynamically add an onload and resize event to the <body> tag. I've looked on the web and in MS documentation unsuccessfuly. Does anyone know the code...
2
mickey0
by: mickey0 | last post by:
HI, is it possible to do this? I'd like at first execute the ONE and then the TWO. Will it work? <body onload="func1()","func2()"> </body>
8
by: Prisoner at War | last post by:
Another weekend, another project, another problem.... Why should a perfectly fine function work only half-way through when called through onClick in an anchor tag?? The text fade-in script...
3
by: PYG | last post by:
Hi everybody I have a simple question : If i use this code : <body style="font-size:24px;color:blue;"> Text in body <table> <tr><td> Text in table
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.