Solved

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

Posted on 2009-04-03
7
490 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now