How to add JavaScript to html

JavaScript, also known as JS, is one of the scripting (client-side scripting) languages, that is usually used in web development to create modern and interactive web-pages. The term "script" is used to refer to the languages that are not standalone in nature and here it refers to JavaScript which run on the client machine.

In other words, we can say that the term scripting is used for languages that require the support of another language to get executed. For example, JavaScript programs cannot get executed without the help of HTML or without integrated into HTML code.

JavaScript is used in several ways in web pages such as generate warning messages, build image galleries, DOM manipulation, form validation, and more.

Adding JavaScript to HTML Pages

There are following three ways in which users can add JavaScript to HTML pages.

  1. Embedding code
  2. Inline code
  3. External file

We will see three of them step by step

I. Embedding code:-

To add the JavaScript code into the HTML pages, we can use the tag of the HTML that wrap around JavaScript code inside the HTML program. Users can also define JavaScript code in the tag (or we can say body section) or tag because it completely depends on the structure of the web page that the users use.

We can understand this more clearly with the help of an example, how to add JavaScript to html

Example

Output:

How to add JavaScript to html

We can also define the JavaScript code in the tags or body section.

Let's understand through an example.

Output

How to add JavaScript to html

II. Inline code:-

Generally, this method is used when we have to call a function in the HTML event attributes. There are many cases (or events) in which we have to add JavaScript code directly eg., OnMover event, OnClick, etc.

Let's see with the help of an example, how we can add JavaScript directly in the html without using the tag.

Let's look at the example.

Output

How to add JavaScript to html

III. External file:-

We can also create a separate file to hold the code of JavaScript with the (.js) extension and later incorporate/include it into our HTML document using the src attribute of the

Now let's create separate JavaScript file

Hello.js

Output

How to add JavaScript to html

Both of the above programs are saved in the same folder, but you can also store JavaScript code in a separate folder, all just you need to provide the address/path of the (.js) file in the src attribute of tag are also not used in it.

The HTML noscript Element

The element provides us an alternate way to create content for the users that either have browsers that don?t support the JavaScript or have disabled JavaScript in the browser.

This element can contain any HTML element other than the

In the above program, we have used the javascript to print the message "Hello World!" and also used element to print the message " Sorry, your browser may not support JavaScript! Or JavaScript is disabled in your browser". The element will print this message only if JavaScript is disabled in the user's browser or his browser does not support JavaScript at all.

Let's see what output we get when we run the above given program in the JavaScript enabled browser.

Output

How to add JavaScript to html

Now, let's see what output we will get when we run the same program in a JavaScript disabled browser (or in a browser that does not support JavaScript).

Output

How to add JavaScript to html

Example 2

Let's run the above given program in the JavaScript enabled browser.

Output

How to add JavaScript to html

Now let's run the above program on JavaScript disabled browser

Output

How to add JavaScript to html
Next TopicHow to enable JavaScript in my browser




Hot Tutorials

Contact US

Email:[email protected]

How to add JavaScript to html
10/30