Solved

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

Posted on 2009-04-03
7
493 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 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