Solved

When is it necessary to declare javascript in the <head> section?

Posted on 2009-04-03
7
494 Views
Last Modified: 2012-05-06
In my PHP website... I include the head inside a file called header.php which I include at the top of each page..  The header.php file opens the html, includes the head and the opening of the body...

On various pages I have different javascript (mainly jquery, but some others) scripts..

My questions:
1) Is it necessary to always include the scripts inside the head of the site... I've have tried declaring the scripts insde the body and they seem to work...yet everywhere tells you to include them in the head...  Please clarify this..

2) If it is necessary to include the files in the head then...
For each page that I use the javascript...do I have to create a custom header and then include the scripts or is there a method to include the scripts in the head?
0
Comment
Question by:Chimeraza
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Accepted Solution

by:
qwerty021600 earned 250 total points
ID: 24058673
It is not mandatory to put javascript in the header but it is advisable. As when the code is rendered, it'll scroll from the head to the body. Before it calls the Javscript from the Body part, the Javascript should already be executed, which will make it to execute faster.
0
 
LVL 13

Expert Comment

by:qwerty021600
ID: 24058702
You need to specify this tag in the <head> part of your page
<script type='text/javascript' src="" language="javascript">

Give path in src ..
0
 
LVL 3

Author Comment

by:Chimeraza
ID: 24058792
Thanks for the response...
so do I need to have a separate head for each document as per my question?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 13

Expert Comment

by:qwerty021600
ID: 24058838
Every page has a head section. On all those pages where you want to use Javascript, call that in their head section as I mentioned it earlier.
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 250 total points
ID: 24059166
Note: the "language" attribute is redundant, and not necessary.

Use this for "inline" scripts:

<script type='text/javascript'>
  ...
</script>

and this for external scripts:

<script type='text/javascript' src='filename.js'></script>
0
 
LVL 3

Author Closing Comment

by:Chimeraza
ID: 31566196
Amen
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24059439
Thanks for the assist, and the points.

Good luck & have a great day
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question