View Full Version : [JS] Body onload in script?
Templarian
June 13th, 2006, 11:59 PM
<body onload="functionname('layer1')">
How do i put that in:
<script type="javascript">
//script here
</script>
Jeff Wheeler
June 14th, 2006, 12:02 AM
I always seem to have trouble doing that, because the body hasn't loaded when the script is executed for the first time. I find calling the method at the end of the body in a short little script works the best…
Templarian
June 14th, 2006, 12:05 AM
even if it doesnt work nice i would still like to know how.
Jeff Wheeler
June 14th, 2006, 12:06 AM
As I said on IM, I think this is what you're looking for (it's untested):
document.getElementsByTagName("body")[0].onLoad = method();
Templarian
June 14th, 2006, 12:20 AM
nope couldnt get it to work.
Jeff Wheeler
June 14th, 2006, 12:27 AM
Maybe you could explain what you're trying to do a bit more. I think my first suggestion was the easiest.
Templarian
June 14th, 2006, 01:01 AM
okay well im loading in a page in the middle of a layout so I can't get to the body tag very easily. And i don't know how to set it up so it works in script tags the same way it would work in the body tag.
Yes nokrev your suggestion is the easiest but I can't get it to work. (mabey show an example with an alert box function i may just be doing it wrong).
g'nite.
bigmtnskier
June 14th, 2006, 01:19 AM
Wait a second... couldn't you just use this??
<script type="text/javascript">
function pageloaded(){
// << insert code here>
}
window.onload = pageloaded;
</script>
-bigmtnskier :ponder:
Templarian
June 14th, 2006, 12:51 PM
wow... I HATE JAVASCRIPT. I was doing exactly what bigmtnskier said but turns out it had to be at the very bottom of the page becuase i was referencing to an object (layer) below it. ... basically can call a function if it hasn't be placed on the screen yet.
Jeff Wheeler
June 14th, 2006, 03:08 PM
That's exactly what I was saying! You can't do that because the body hasn't loaded yet, so anything you reference won't exist…
Ah! :lol:
Edit: This is what I'm saying…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Austin's Example</title>
<script type="text/javascript" charset="utf-8">
function my_func () {
alert("contents: "+document.getElementById("content").innerHTML);
}
</script>
</head>
<body>
<p id="content">Some content...</p>
<script type="text/javascript" charset="utf-8">
my_func();
</script>
</body>
</html>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.