Home » How to Link JavaScript to HTML? – HTML Script src Attribute
How to

How to Link JavaScript to HTML? – HTML Script src Attribute

How to Link JavaScript to HTML - HTML Script src Attribute

Are you confused about How to Link JavaScript to HTML? Don’t have technical or coding knowledge? Don’t worry. I will give you the best and easy solution to do that. Even more, No need to have technical or coding knowledge.

Why do you need to link JavaScript file to HTML?

The answer is: when you are working on web design or development projects, then we recommend you to make the separate CSS and JS files. You have to link those files to your HTML file to run CSS styles and JavaScript code on the front end.

Where you have to place the JS link code in HTML?

Generally, CSS styles are included in between <HEAD> and </HEAD> tag. You can write anywhere in between the HEAD section. Furthermore, JavaScript files are included in footer of HTML code or above </BODY> tag. Also, you can add JS files inside <HEAD> tag. But sometimes it will generate some error in some cases. So, It is better to place at the footer of the HTML page.

How to Link JavaScript to HTML

Linking of JavaScript file to HTML is very simple. Just need to place only one line which is really very simple to write. You have to use <script> tag with “SRC” Attribute to do that. You can see it in below code snippet.

1. Link Direct JS File

<script src="myscripts.js"></script>

Note: Replace the “myscripts.js” file name with your JS file name.

2. Link File Inside Folder

If the javascript file is inside the folder then you have to write the code like this.

<script src="foldername/myscripts.js"></script>

Note: Replace folder name with your actual folder’s name and myscript.js with your original JS file.

3. If HTML and JS Both Inside Folder

If your HTML file is not in the root or it’s inside the folder and js file is in another folder. Then you have to write the script like below.

<script src="../foldername/myscripts.js"></script>

“../” will push you inside the parent directory. So, the server can get access to the JS file and run on the client side.

Finally, you have the solution to your question: How to Link JavaScript to HTML?. You can read above all points and get your solution.

I hope you will like this article. If you like it, please share it with your friends.

Related Articles: