Using External Scripts

The problem with using scripts on the HTML page, as in the last example, is that the script is only available to that particular page. That’s why those kinds of scripts are sometimes called internal scripts. But often, you’ll want multiple HTML pages to share a script. You do this by including a reference to an external script, that is, a separate file that just contains JavaScript. This external file is called a .js file, because whatever it’s called, the file name should end with the suffix .js. Individual pages call the .js file simply by adding a new attribute, src, to the script tag.

This saves a lot of code on every page and, more important, makes it easier to maintain your site. When you need to make changes to ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.