Solved

Location of Javascript on HTML page

Posted on 2016-11-01
3
44 Views
Last Modified: 2016-11-02
I have been doing websites for over 20 years. Traditionally, I always thought that the Javascript had to be in the document <head> section.

I gather now it can be anywhere, paired by <script>......</script>

Is that true?
0
Comment
Question by:Richard Korts
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 41869382
Yes, though it's good practice to add "async" or "defer" for scripts that can load asynchronously or be deferred until after page load, to speed load time.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 41869464
(No points, just wanted to clarify) Yes it can be anywhere. The key point too is that putting it in the head means the browser will typically load the script and run it before your page has loaded. This can not only cause errors but also increase the time it takes for your page to load.
It's become accepted practice to put scripts at the bottom of the page, just before the body end tag. That way the browser does not "see" that script until your page (the DOM) is loaded
1
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41869777
Addendum to Rob and Terry's post.

There can be an order requirement in how you place scripts.

For example
<script>
$(function() {
  $('.someclass').click();
});
</script>
<script src="http://code.jquery.com/jquery.js"></script>

Open in new window

Will generate an error because $(function) uses jQuery which the browser does not know about yet.

If you are emitting jQuery or JavaScript with a library dependency and you put your scripts before </body> you could run into this problem. The solution is to place in the head or to cache your script output and emit it all at the end.

Having said that random script output in my view is bad design so a well designed page should not run into these problems.

If you are really interested in performance you would pack your JS and CSS files into single files for final deployment of your site so there is only one file which you could then include.
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

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.
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 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

8 Experts available now in Live!

Get 1:1 Help Now