Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

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

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
Chimeraza
Asked:
Chimeraza
  • 3
  • 2
  • 2
2 Solutions
 
qwerty021600Commented:
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
 
qwerty021600Commented:
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
 
ChimerazaAuthor Commented:
Thanks for the response...
so do I need to have a separate head for each document as per my question?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
qwerty021600Commented:
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
 
HonorGodCommented:
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
 
ChimerazaAuthor Commented:
Amen
0
 
HonorGodCommented:
Thanks for the assist, and the points.

Good luck & have a great day
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now