Open In App

How to define the URL of an external script file in HTML5 ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to define the URL of an external script file inside HTML. This may be needed when a large script code is needed to be included, and it may create confusion if all the code is written in a single file. Also, there may be a requirement for the same script to execute on different web pages, and rewriting the same code can lead to redundancy. The code can be refactored by simply writing the script code in a separate file and defining it in the HTML file.

Approach: The script tag has an attribute src which is used to specify the path of an external script file.

Syntax:

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

Example: In this example, the examplescript.js external script to be included in the HTML file.

examplescript.js




alert("The script file URL is successfully defined");


index.html




<html>
  <body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
      
    <!-- Including an external script -->
    <script src="examplescript.js">
    </script>
  </body>
</html>


Output: On loading the page in our browser, we will get a pop-up with the defined message which confirms that the external script has properly loaded.


Last Updated : 24 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads