Javascript - Code implementation

December 2016



Where to insert the JavaScript in your HTML page?


Ther are several ways to insert JavaScript in an HTML page:

Using the <SCRIPT> tag


The JavaScript code can be inserted wherever you want in your web page, however, you must ensure that the browser has fully loaded before your script before executing an instruction. Indeed, when the browser loads your web page, it processes from the top to the bottom. In most cases, we try to put the maximum number of elements of the script in the header (between the <HEAD> and </HEAD>). Javascript events will in turn be placed in the body of the page (between the <BODY> and </BODY> tags) as an attribute of an HTML control ...
<SCRIPT language="Javascript">

 <!--

Put your code here

// -->

</SCRIPT>

The argument of the <SCRIPT> tag describes the language used. It can be "JavaScript" "JavaScript1.1" "JavaScript1.2".
It can also be used for other programming languages (e.g VBScript).
To use different versions of JavaScript, while retaining a certain level of compatibility, simply declare multiple SCRIPT tags having each as parameter a corresponding version of JavaScript.

In an external file


It is possible to put JavaScript code attached to a file. The code to insert is as follows:
<SCRIPT LANGUAGE="Javascript" SRC="url/file.js"> </SCRIPT>

Where url/file.js corresponds to the path to the file containing the JavaScript code.

Using events


An event is a user action, such as mouse click:
<balise eventHandler="Javascript to be inserted">

Where eventHandler is the name of the event.

Orignal document published on CommentcaMarche.net

Related :

This document entitled « Javascript - Code implementation » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.