All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Link JS File In HTML

Last Updated : Mar 11, 2024

How To Link JS File In HTML

In this tutorial we will show you how to link JS file in HTML, in HTML, there is also a property to link a js file in html like CSS where js is known as JavaScript. So, JavaScript is mostly used to make a responsive website.

Now, in this session we will learn that how we are able to link a js file in html.

Step By Step Guide On How To Link JS File In HTML :-

As, there are three ways to add js (JavaScript) in html. That are:

  • Inline JavaScript
  • Internal JavaScript
  • External JavaScript

And first two from them do not generate any external file so that there is no any generation of link to add in html file.

So, the last one that is external js file, in which an external file is created which need to link in html file. But except the concept of linking file, all the work is same in all js file types.

<!DOCTYPE html>
          <title> Title of the document<title>
		<script src=”name of js file with .js extension”>
	It is a file with JavaScript. JavaScript file is linked above
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Thirdly, <body> tag is used to define the webpage body. All the contents to show on website are written here.
  5. Here, in body tag first we linked the JavaScript file. For linking this file, we create a script tag. Script tag is mostly used to link any JavaScript file but in body tag, not in head like CSS files.
  6. And in JavaScript file, we have to write about the required codes, that we need in html file and after this we have to save the JavaScript file with .js extension. You can save this file in any other folder, at desktop and at same location where your html file is.
  7. Now, the script tag that we create in html, in this we have to write about the source of JavaScript file, that where it is saved, here we have to specify full path with file name and its extension.
  8. And you can also use this script tag anywhere in the body, it’s not mandatory to use a top of page and after this you are able to write your html codes as you do.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

In conclusion, we can say that we have to link the JavaScript file using script tag.

And when you open this webpage in your browser, the result will be shown there that how the JavaScript codes work. I hope this tutorial on how to link JS file in HTML helps you.