WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2004, 09:15 PM
wwwizzard wwwizzard is offline
WebProWorld Member
 

Join Date: Apr 2004
Location: Brisbane, Australia
Posts: 46
wwwizzard RepRank 0
Default Why does <script> have to be within <head> tags?

Hi everybody
Whenever you copy and paste a script, whether it be javascript or any other script, the instructions are always to include the <script> itself within the <head> tags.
I sometimes find it easier to include the <script>***</script> somewhere in the middle of the page right above where you actually use the script.
I have checked across various browsers and I cannot find any reason why it has to be within the <head> tags. It all seems to work regardless where you have the <script>.
Does anybody know the reason? Does it really matter?

Cheers
Reply With Quote
  #2 (permalink)  
Old 07-23-2004, 09:24 PM
ronniethedodger's Avatar
ronniethedodger ronniethedodger is offline
WebProWorld 1,000+ Club
 

Join Date: Aug 2003
Location: Central US
Posts: 1,576
ronniethedodger RepRank 0
Default

Nope, it doesn't matter as far as functionality. I think maybe in the past it did.

One reason though is to keep it out of the < body > of the page. It is better to put the script in an external .js file though, and reference that in the < head > (cannot to that in the body).

Some calls to script functions do appear in the body though ... such as rollover functions. Some scripts have to be in the body also, such as a newsfeed or some other type of Html insertion. Some external free counters are the same way.
__________________
Ronnie T. Dodger
Web Design Forum-Web Design & Development News
Reply With Quote
  #3 (permalink)  
Old 07-26-2004, 11:58 AM
swstyles's Avatar
swstyles swstyles is offline
WebProWorld Pro
 

Join Date: Aug 2003
Location: Phoenix, AZ
Posts: 167
swstyles RepRank 0
Default

For the most part, its just good coding practice. As a coder I appreciate good coding practices. I have found that some scripts work better being embedded within the document. Some scripts won't work if they are not embedded in the <head>.

It depends on what you are doing.
__________________
Daniel M: South West Styles/STEELid
daniel@steelid.com
http://www.steelid.com
http://www.thephototool.com
Reply With Quote
  #4 (permalink)  
Old 07-27-2004, 05:52 AM
wwwizzard wwwizzard is offline
WebProWorld Member
 

Join Date: Apr 2004
Location: Brisbane, Australia
Posts: 46
wwwizzard RepRank 0
Default Thanks

Thanks guys, what you're really saying is if it works, it works. Right?
Just what I thought, but you never know!

Cheers, and thanks!
Reply With Quote
  #5 (permalink)  
Old 07-31-2004, 01:23 AM
ADAM Web Design's Avatar
ADAM Web Design ADAM Web Design is offline
WebProWorld 1,000+ Club
 

Join Date: Dec 2003
Location: Toronto, Ontario, Canada
Posts: 2,217
ADAM Web Design RepRank 0
Default

A Javascript will work anywhere in the <head> or <body> of a document. While it is preferential to put the JScript in the head of a document (and in a separate .JS where possible), sometimes it is necessary or easier to put the JScript into the body of the document (e.g. if you're dynamically generating an array of pics to be used in conjunction with a randomly rotated image section).

It may not provide output code that HTML coders would like, but for the actual programmers it may be easier to understand.

As swstyles pointed out, some JScripts (such as a DHTML menu) will only work within the body of a document.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: ltheadgt, ltscriptgt, tags, within



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Search Engine Friendly URLs by vBSEO 3.0.0