I know you can use the onload statement to start a javascript when the page has loaded, but is there a statement that can start a javascript as soon as the page starts to load?
| SitePoint Sponsor |

I know you can use the onload statement to start a javascript when the page has loaded, but is there a statement that can start a javascript as soon as the page starts to load?
I am not an expert, but if you put a javascript statement into the head section of a file eg
<html>
<head>
<script language="javascript">
<!--
function dothis()
{
// include stuff here
}
dothis();
// -->
</script>
</head>
<body>
<!-- Body of your html -->
</body>
</html>
Then dothis() is run before loading the rest of the file.
[mmj] My magic jigsaw
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Bit Depth Blog · Twitter · Contact me
Neon Javascript Framework Jokes
I'm not so sure if the RUNAT will work, but don't take my word for that. I was under the impression that specifying RUNAT="server" is used for server-side scripting, such as ASP (although it is much more commonly used implicity as simple <% %> delimiters). However, just entering code in the head, below the title and before the style sheets will run code as soon as it is downloaded. Be careful, as this can cause errors! Hope I helped!
Ok ...
Firstly, don't put any functions in header if you want them to run when the page starts to load because functions only work when they are called by an event-handler.
If you want to use a function (in order to make it common to multiple pages) put the function in a seperate JS file, and then call the function from an inline-script inside the head of the document. Like thus ...
<html>
<head>
<script language=javascript type="text/javascript" src="yourfilename.js">
<script language=javascript type="text/javascript" >
<!-- // hide from old browsers
window.load = yourFunction() ;
// -->
</script>
</head>
That should work just nicely ...
Cheers. Scot-Bot. )
</head>
Bookmarks