How to link JavaScript to HTML?

JavaScript is a well-known scripting language that’s used both on the client-side and on the server-side. JavaScript is important for our web pages as it ensures we have an interactive web page. With web pages, you need to do more than just display content. You are required to load JavaScript. Among the first JavaScript, files to be added is Google Analytics which helps in tracking page loads & visitors.

What is javascript?

JavaScript, often abbreviated JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. Over 97% of websites use JavaScript on the client-side for web page behavior, often incorporating third-party libraries.

What is JavaScript and why it is used?

Javascript is used by programmers across the world to create dynamic and interactive web content like applications and browsers. JavaScript is so popular that it’s the most used programming language in the world, used as a client-side programming language by 97.0% of all websites.

Link javascript to Html

One can add JavaScript-code in a HTML document through employing a devoted HTML tag <script> which wraps in JavaScript code.

A <script> tag can be positioned at the <head> section of HTML or at the <body> section. This depends on when you need to load the JavaScript. A JavaScript code can move inside the document <head> section for it to be contained & out of the major content of the HTML document

Linking JavaScript files to HTML is simple. All that’s needed is to place just one line that’s simple to compose. You need to utilize <script> tag together with “SRC” Attribute to achieve this.

A). Link Direct JavaScript File

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

Remember to replace “myscripts.js” with your JavaScript file name.

B). Link-File Inside Folder

When the JavaScript file is in the folder you need to write the code this way

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

Replace the folder name with an actual folder’s name & myscript.js using your original JavaScript file.

C). If HTML & JavaScript all Inside Folder

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

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

../” moves you inside the parent directory. Thus, the server offers you access to JS file & operate on the client-side.


JavaScript added below <head> tags

<!DOCTYPE html> <html> <body> <h2>JavaScript ISO Dates</h2> <p id="demo"></p> <script> const d = new Date("2015-03-25"); document.getElementById("demo").innerHTML = d; </script> </body> </html>


JavaScript ISO Dates

Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)


Join Telegram Join Whatsapp

Leave a Comment