Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

My Personal Notes arrow_drop_up
Last Updated : 24 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials