External JavaScript

If you are going to define a functionality which will be used in various HTML documents then it’s better to keep that functionality in a separate JavaScript file and then include that file in your HTML documents. A JavaScript file will have extension as .js and it will be included in HTML files using <script> tag.

Note: Do not add <script> tag in the external file, and provide the complete path where you have put the JS file.

Example – HTML File

<!DOCTYPE html>
<html>
 
   <head>
      <title>This page title</title>
      <script src = "https://www.lecturely.com/wp-content/themes/divi-child/asset/external.js" type = "text/javascript"/></script>
   </head>
   <body>
      <input type = "button" onclick = "Hello();" name = "ok" value = "Click Me" />
   </body>
</html>

Example – External Script File

function Hello() {
   alert("Hello, Javascript World");
}

Output