What html tag is used to indicate that we are embedding javascript code


Example

Write "Hello JavaScript!" with JavaScript:

Try it Yourself »


Definition and Usage

The


Global Attributes

The tags in an HTML document, it becomes inactive.

Certain JavaScripts do not work with all the browsers and sometimes a script works on and above (or vice a versa) a particular version of a web browser.

The script Tag

The script is an HTML element. Html script element is used to enclose client side scripts like JavaScript within an HTML document.

Syntax

There are four types of attributes in script element:

1. language

The language attribute is used to specify the scripting language and it's version for the enclosed code. In the following example, JavaScript version is 1.2. If a specific browser does not support the said JavaScript version, the code is ignored. If you do not specify a language attribute, the default behavior depends on the browser version. The language attribute is deprecated in HTML 4.01.

Example

  

2. src

This attribute specifies the location of an external script. This attribute is useful for sharing functions among many different pages. Note that external JavaScript files contain only JavaScript statements and files must have the extension .js.

Example

  

3. defer

If you set defer attribute, the browser delays the execution of the script or it changes the order of the execution of the script. This can improve the performance by delaying execution of scripts until the content of body is read and displayed by the browser.

4. type

This attribute specifies the scripting language. The scripting language is specified as a content type (e.g., "text/javascript" ). The attribute is supported by all modern browser.

Example

  

The noscript tag

If any browser does not support the JavaScript code the alternate content placed within noscript tag is being executed.

Example

 

Javascript in HTML document

There are two general areas in HTML document where JavaScript can be placed. First is between ...... section, another is specific location in ...... section. If you want to display a message 'Good Morning' (through the JavaScript alert command) at the time of page loading then you must place the script at the ...... section. In the following examples you will see the different location of tags in a HTML document.

Script in the Head


  
  
   Script in head section 
  
  
  
  
   

Script in the Body


  
  
   Script in the Body 
  
  
  
  
  
  

Scripts in the Head and Body


  
  
   Script in head and body section 
  
  
  
  
  
   

Two Scripts in the Body


  
  
   Two Scripts in the Body 
  
  
  
  
  
   
 

Note: Optionally, if your script is not required to be executed before the content of the body is displayed, and your script takes longer time to load objects, you can place your script at the end of the body element.

Double or Single Quotes in JavaScript

There is no preferred method, you can use either. If you use one form of the quote (either single or double) in the string, you may use other as the literal.

Previous: JavaScript and ECMA Specification
Next: JavaScript Syntax and comments

JavaScript: Tips of the Day

Object.freeze

const box = { p: 10, q: 20 };

Object.freeze(box);

const shape = box;
shape.p = 100;

console.log(shape);

Object.freeze makes it impossible to add, remove, or modify properties of an object (unless the property's value is another object).
When we create the variable shape and set it equal to the frozen object box, shape also refers to a frozen object. You can check whether an object is frozen by using Object.isFrozen. In this case, Object.isFrozen(shape) returns true, since the variable shape has a reference to a frozen object.
Since shape is frozen, and since the value of p is not an object, we cannot modify the property p. p is still equal to 10, and { p: 10, q: 20 } gets logged.

Ref: https://bit.ly/3jFRBje

How do you tag JavaScript in HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag tags. From an external file specified by the src attribute of a tag of the HTML that wrap around JavaScript code inside the HTML program.