Skip to content

DataOps Blog

Just another DataOps site

  • Home
  • certification
  • consultant
  • consulting
  • Contact Us!
  • courses
  • Intro to Laravel
  • tools
  • trainer
  • training

How to add JavaScript to HTML?

December 22, 2021
By Manish Kumar In JavaScript

How to add JavaScript to HTML?

JavaScript Versions: How JavaScript has changed over the years

JavaScript is a light weight, interpreted and easy to lean programming language. JavaScript is an open source and run on browser, it is designed for creating network centric application.

JavaScript is very easy to implement because it is integrated with HTLM.

It is first discovered by “Brenden Eich” in 1995, JavaScript is also known as ECMAScript. Currently the update version is ECMA6. 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 possibly 3 ways in which a user’s can add JavaScript to HTML pages. These steps are:

  • Embedding code
  • Inline code
  • External file

We will see three of them step by step

II. Embedding code:-

To add the JavaScript code into the HTML pages, we can use the <script>…..</script> tag of the HTML that wrap around JavaScript code inside the HTML program. Users can also define JavaScript code in the <body> tag (or we can say body section) or <head> 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:-

This is not it, we can also define the JavaScript code in the <body> tags or body section.

Let’s understand through an example.

Example:-

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 <script>…. </script> tag.

Let’s look at the example:-

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 <script> tag. It becomes very helpful if we want to use the same code in multiple HTML documents. It also saves us from the task of writing the same code over and over again and makes it easier to maintain web pages.

In this example, we will see how we can include an external JavaScript file in an HTML document.

Let’s understand through a simple example:-

Function code written in external .js file which we connect with html files.

Code in .js file is

function display() { 

alert(“Hello JavaScript!”); 

} 

Wrapped up

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 <script> tag. External JavaScript file only contains JavaScript code and nothing else, even the <script>…. </script>tag are also not used in it. I hope you like this particular information about addition of JavaScript file helpful.

Keep practising and Exploring.

Thank You!!

css html javascript jquery MySQL
Written by:

Manish Kumar

View All Posts

Recent Posts

  • The Online Dating Association (ODA) Maintains the Gold Standard for online dating sites in britain
  • Tips Balance On The Internet And Typical Dating
  • meet black gay men Gay Hookups
  • (no title)
  • Online Casinos Offer No Deposit Casino Bonus Codes
  • A Board Website Review
  • How you can Remove Disease From ipad device
  • Dating – When You Should Discuss Dirty Little Methods?
  • How to Find the Top Canadian Online Casino
  • Research Papers For Sale
  • Free Slot Machine Games – Tips to Play Slots Free
  • (no title)
  • Why Use a VPN Online?
  • The very best GTA Games
  • DesignLab Xbox 360 – Create Your Very Own Controllers
  • Tips on how to Conduct Individual Businesses
  • Tips on how to Download the Best Android Programs
  • Finest Antivirus Totally free
  • VPN USA — Protect Your web Privacy and Unblock Geo-Restricted Content
  • The right way to Download Android os VPN

Archives

Categories

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
  • Free Video Tutorials
  • DevOps School
  • Best DevOps
  • scmGalaxy
  • Artificial Intelligence
  • DataOps
  • AIOps
  • GuruKul Galaxy
  • DevOps Consulting
  • DevOps Freelancers
  • DevOps Trainer
  • Free Ebooks
  • School for Debugger
  • Holiday Landmark
  • Surgery Planet
  • My Hospital Now
  • My Medic Plus
  • ProfessNow
  • Cotocus
  • Stocks Mantra
  • I Reviewed

Proudly powered by WordPress | Theme: BusiCare by SpiceThemes