how to use js file in html code example

Example 1: include js in html

<html>
    <head>        
    </head>
    <body>
    
        <script type="text/javascript" src=es4.js></script>
           
    </body>
        
</html>

Example 2: calling javascript file in html

<script type="text/javascript" src="yourfile.js"></script>

Example 3: how to add script in html head

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

<!--just add defer attribute so script will execute at last-->

Example 4: link javascript to html

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

Example 5: How to add js file to a site through url

var jsFile = document.createElement("script");
jsFile.src = "homePage.js";  // it can be path also  "{themes('/scripts/homePage.js')}"
document.body.appendChild(jsFile);  //append it as src to body

Tags:

Html Example