JS 03: JavaScript external file

Javascript also can be written in a external file. An external file is then included in a page in order to use them. In this way same java script can be used across multiple pages.

Another reason to use external file is to separate the HTML code and JavaScript code because sometimes it might expand to long line of code and make HTML code hard to read.

for example, above script is moves to external file called myScript.js

function DisplayGreeting(){
  alert(“Hello world”);
}

to include this file

<head>
  </script type = “text/javascript” src = “myScript.js”></script>
</head>

JavaScript code can  be included anywhere in the HTML code, but often they are included in head section. Another most common place to include the JavaScript at the end of body section but after loading all the element of the page.


<body>
  <h1>JavaScript external file example</h1>
  <!-- rest of the html codes -->

  </script type = “text/javascript” src = “myScript.js”></script>
</body>

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net