External JavaScript

If you have completed the beginning JavaScript pages then you have been putting the code into the Web page itself either in the HEAD section or actually into the tags. This is very similar to embedded CSS and in-line CSS and with the same disadvantages.

Moving your scripts to an external file

If you put the JavaScript in an external file then many pages can access the same code. To link to the external file just use SCRIPT tags with a SRC attribute:

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

Now you can access the JavaScript as normal by calling the functions in the file. Go to one of your existing JavaScript pages and cut a function from the HEAD section. Paste it into an external file and save it with any name that ends with .js. Add the SRC attribute to the SCRIPT tags. You shouldn't need to change anything else for it to work as before.

You can link to more than one file by using multiple SCRIPT tags. The order can matter if functions in one file depend on those in the other.

Using other people's JavaScript

Don't get sidetracked by this but there is a lot of JavaScript out there which may do what you want without you having to write it. The only real problem with this is that you won't learn much and someday you'll want to change the code to do something unique and you won't know how.

Some of these JavaScript libraries are even available from other people's servers so you don't need to take up your server space and bandwidth. There are many out there but here are a few of the best.

All of them aim to work on any browser and to be fully compliant with Web standards.

The links above will explain how to use the libraries on your site.

submit to reddit Delicious Tweet