External scripts

Javascript stored in external files is available to any web page that calls it.
That means many web pages can reuse the same code over and over without rewriting it.

Using this method we have two files, the html page and the file containing the javascript.

Here is the content of the external javascript file external.js

function hi(){
    alert('Hi there');
}

Here is how we call the external javascript file from within the html page

The html file

<head>
<script src="external.js" type='text/javascript'></script>
</head>
<body>
<div onclick='hi();'>Click Me</div>
</body>

As you can see in the html page the call to the function is the same as before.

Click Me
to call an External function an example of it working.

Exercise

Create a page with the above code and make sure it works.

Footnote

Note: In HTML5 the type='text/javascript' part is optional