Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to put javascript code anywhere in HTML code?

Tags:

I see that Javascript code is normally in heading part of HTML code.

<head> <script type="text/javascript" language="javascript" src="core.js"></script> ... </head> 

Is it OK to put the Javascript code in a body part of HTML code? I tested it, but it seems to work.

<body> <script type="text/javascript" language="javascript" src="core.js"></script> ... </body> 

If so, why the examples of Javascript books put the javascript code in heading part? If not, what's the difference between putting the javascript code in body/heading part?

like image 977
prosseek Avatar asked Dec 09 '10 14:12

prosseek


People also ask

Can you put JavaScript anywhere in HTML?

JavaScript in <head> or <body>You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Where should I put my JavaScript code in HTML?

Using the script tag to include an external JavaScript file You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Does it matter where you put JavaScript?

If it is using the document. write() method, then it does matter where it is on the page. If it's trying to reference elements in the DOM, it is best put in the HEAD and have those functions that access DOM elements triggered after the page is loaded.

Can we add JavaScript code in HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.


2 Answers

Not only is it OK, it's actually better, since it lets the content come first.

If your viewers have a slow (eg, mobile) connection, it's better to send the actual content first, so that they can read it while the browser downloads the Javascript.

like image 53
SLaks Avatar answered Sep 21 '22 12:09

SLaks


All the people saying it is better only applies if you are talking about at the bottom of the page (and that is an up for debate thing) from a code quality point of view, it is NOT ok to sprinkle script tags through your html. All references to javascript should be in a single place on the page, either the head (where they should be), or the very bottom (as a perf optimization)

Edit:

Basically, a web page is made up of 3 pieces; style (css), structure (html), and behavior (javascript). These pieces are all very distinct, so it makes sense to keep them as separate as possible. That way if you need to change some javascript, it is all in one place. If it is sprinkled through the file, it becomes much more difficult to find the code you are looking for, and that code basically becomes noise when you are just looking at structure.

It is the same arguments as why not sprinkle db access code all over your page. It has nothing to do with technical reasons, purely an architectural/design decision. Code that does different things should be kept separate for readability, maintainability, and by extension, refactorability (not sure if that last one is actually a word...)

like image 23
Matt Briggs Avatar answered Sep 20 '22 12:09

Matt Briggs