Categories
Saunas

How to add javascript to your website using html

JavaScript is one of the scripting languages which is used to make interactive and modern web pages. Without any need of additional plugins, modern web browsers support JavaScript with built-in engines.

Furthermore, while working with web files, JavaScript needs to be executed along with HTML. This can be done either by including inline JavaScript code within the tag of the HTML file or you can add JavaScript through an external file. JavaScript tag either used in the head section of HTML file or in the body section, it only depends where you want to load JavaScript. JavaScript can be written within tags to keep out of the main content of HTML files. If you need JavaScript to execute across a web page layout then the script is usually called into the body section.

JavaScript can be used in numerous ways in web pages like validation of the form, creating warning alerts, and much more. In this article, we will go through how to incorporate JavaScript into HTML files in depth.

Following are the ways to incorporate JavaScript into HTML files.

Embedding code

The …tag is used to add JavaScript into web pages which helps to wrap JavaScript within the HTML programs. The code of Javascript can be either under the body section or head section depending on the structure of the application.

Example1: JavaScript in head section

Output:

How to add javascript to your website using html

Example2: JavaScript in the body section

document.write ( “JavaScript added in body section” ) ;

< p > This example shows how to include JavaScript in the body section < / p >

Output:

How to add javascript to your website using html

Inline code

When you need to call a function into HTML attributes then an inline code of JavaScript is used. JavaScript code can be used directly in various events like OnMouseover(), OnClick(), and many more. Main points should be kept in mind while using inline scripts:

  • Useful for small scripts or scripts which only execute within one file.
  • Difficult to read and understand if scripts are larger or used in several pages.

Example1: This example shows how to incorporate JavaScript into HTML without tags.

< p > This example shows how to include inline JavaScript without script tags. < / p >

Output:

How to add javascript to your website using html

Example2:

In the above example, whenever the user loads the web page the script displays an alert message showing the current date.

Output:

How to add javascript to your website using html

External file

JavaScript code can be written in a separate file with an extension of “.js” and then included in the HTML file. JavaScript file name can be included in the “src” attribute of the script. External files of JavaScript code can be reusable. Therefore, separate files for JavaScript code save you from rewriting the same software program. External files are much easier to manage.

JavaScript, also abbreviated to JS, is a programming language used in web development. As one of the core technologies of the web alongside HTML and CSS, JavaScript is used to make webpages interactive and to build web apps. Modern web browsers, which adhere to common display standards, support JavaScript through built-in engines without the need for additional plugins.

When working with files for the web, JavaScript needs to be loaded and run alongside HTML markup. This can be done either inline within an HTML document or in a separate file that the browser will download alongside the HTML document.

This tutorial will go over how to incorporate JavaScript into your web files, both inline into an HTML document and as a separate file.

Adding JavaScript into an HTML Document

You can add JavaScript code in an HTML document by employing the dedicated HTML tag tags. These tags define JavaScript code.

  • It is suggested to only implement short JavaScript codes inside the tag. Otherwise, the code can become hard to read.
  • JavaScript file extension is .js .
  • Inline JavaScript

    There are two ways to include JavaScript in your HTML document. The first is by writing JavaScript code within