FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Why some script tags must put in the head of document?

 


Mgccl
I got some script files, each one of them need different way to make it work
some of them tells you you must put them inside the head tags, or they won't work.
another ones I found don't need to put in the head...
Why?

I also found some of the event of some script must active in body tag, like body onload something happens...
like for drag and drop JS API, you must put the codes that telling how the dragable DIV work just before the closing tag of body, or it won;t work....
Why is that? how do they do it? and is there any way to make those script change to the place I want them to be not what they should be and still make them work?
Stubru Freak
Mgccl wrote:
I got some script files, each one of them need different way to make it work
some of them tells you you must put them inside the head tags, or they won't work.
another ones I found don't need to put in the head...
Why?

I also found some of the event of some script must active in body tag, like body onload something happens...
like for drag and drop JS API, you must put the codes that telling how the dragable DIV work just before the closing tag of body, or it won;t work....
Why is that? how do they do it? and is there any way to make those script change to the place I want them to be not what they should be and still make them work?


I think it's mainly because if a function call is downloaded, but the function itself isn't yet, the script won't work. So I think they say in the head just because you're sure it's downloaded before the body then. But not sure.
shalmoli
Some of the scripts need to be executed earlier than the other scripts. As rightly pointed out in the earlier mail, it may be a function . It may be variable declation/initiation. Moreover certain php directives need to be called at the top of the page e.g. session variables like session_register, session_start,etc.
Nyizsa
Scripts in the <body> tag are executed upon loading (they produce the page output). Scripts in the <head> tag are executed upon call. Usually event-triggered scripts are here, and they are called by the elements' event handlers.
For example, in the <body> tag you may write the current date.
In the <head> tag you may put a script you verify a form input with. And you call it this way:
Code:
<input type="button" onclick="verify()" value="Verify" />
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.