A+ A-

What is external Javascript, How it is used?

External Javascript

External Javascript is where things get interesting. Any time you have a block of code which you will want to use on several different web pages you should place that block in an external Java script file. The clock on the upper right-hand corner of this page is a good example. The clock appears on almost every page on this site and so it is included in my "common.js" file. Every web-page on the site will load this file and so the clock is available to all of my web-pages.

There's nothing fancy about an external Js file. All it is, is a text file where you've put all
your Javascript. Basically everything that would ordinarily go between the <script> tags can go
in your external file. Note that between was stressed, you can not have the <script> </script> tags themselves in your external file or you will get errors.

[code type="JavaScript"]
<script type='text/javascript' src='common.js'>
</script>
[/code]

The biggest advantage to having an external Javascript file is that once the file has been loaded,
the script will hang around the browser's cache which means if the Javascript is loaded on one
page then it's almost a sure thing that the next page on the site the user visits will be able to load the file from the browser's cache instead of having to reload it over the Internet (This is an
incredibly fast and speedy process).

Including an external file is basically the same as doing an in-line script, the only difference is
that you specify a filename, and there's no actual code between <script> and </script>..

When the browser encounters this block it will load common.js, evaluate it, and execute it. Like
in-line scripts above you can place this block anywhere you need the script to be and like in-line
scripts you should place these as close to the bottom of the web-page as you can get away with.
The only difference between in-line Javascript blocks and external Javascript blocks is that an
external Javascript block will pause to load the external file. If you discount that one thing,
there's no procedural difference between the two!

External Javascript is where things get interesting. Any time you have a block of code which you will want to use on several different web pages you should place that block in an external Java script file.