Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the main function...

Status
Not open for further replies.

PogoWolf

Programmer
Joined
Mar 2, 2001
Messages
351
Location
US
I have a little piece of code that I'm trying to understand for a project that I'm on. The program has a Onload function, and a fucntion labeled 'main'

IS this the same as the 'main' function in C??

Such as.. after the Onload code is fired off, will the code in the Funcation labeled 'Main' fire off right after it?
The PogoWolf
 
If your talking about Js only, then the onLoad event will fire off any function called there when the page loads, everytime...
Not sure what C would do... I have not failed; I merely found 100,000 different ways of not succeding...
 
Ok, so the onload will start the JS program up and running.. and 'main' is just the name of a function, with nothing special about it..

I just wasn't sure if there was a special name you could give a function to have it fire off as the very first part of the program. Guess not, that would be the Onload. =)

Thanks!!!!!!

The PogoWolf
 
No problem, you can call your function anyname you want, but I reccomend that you do not call your function any restricted names (like: body, math, if, match, etc...)
You won't get a error, but there is a chance that the browser can get confused and either not run the function at all or have some wierd results instead...
But for the most part, that is how the onLoad works...:-) I have not failed; I merely found 100,000 different ways of not succeding...
 
Without looking at the sourcecode, I can't say what the intended usage of these functions is. However, This is how I would implemment these functions in my code...

<html>
<head>
<SCRIPT language=&quot;JavaScript&quot;>
<!--

// main() - An attempt to maintain a Structured Script
// NOTE: All statements should be made within
// function calls or as javascript:function
// call within the HTML

main(); // First Command to be executed
function main()
{
alert(&quot;Executing..... main()&quot;);
}

// onLoad()
// Invoked from tag: <Body onLoad(&quot;onLoad()&quot;)>
function onLoad()
{
alert(&quot;Executing..... onLoad()&quot;);
}

//-->
</SCRIPT>
</head>
<body onLoad=&quot;Main()&quot; bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
</body>
</html>

Script Usage:

Main():

A call to main()is the first line to be executed and is called between <head> and </head>. This routine is generally used for variable and some object initialization This routine is placed between the <head> and </head> tags to allow the user to make document.write() function calls.

onLoad():
Invoked from tag: <Body onLoad(&quot;onLoad()&quot;)>
This event fires after the </body> of a document has been drawn, and provides a convenient place to create object references for the components that are exposed by the browser's DOM (Document Object Model).

Notes:
(1) document.write() will fail if called after the </body> of a document has been drawn. Use this function from main()
(2) DOM references must be created in or after the onLoad event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top