Solved

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

Posted on 2009-04-03
7
492 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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

895 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

13 Experts available now in Live!

Get 1:1 Help Now