Loading JavaScript from External Files

<head>   <script type="text/javascript"     src="scripts/scriptA.js"></script>   <script type="text/javascript"     src="scripts/scriptB.js"></script> </head>

As you create more and more complex JavaScript web applications, you will find that adding the JavaScript to your HTML files doesn’t make much sense. The files become too big, and you will often want to reuse the scripts in other web pages.

Instead of including the JavaScript inside the <script> tag, you can specify a src location for the script to be loaded from. When the browser encounters a src attribute in the <script> tag, it downloads the script from the server and loads it into memory.

Get jQuery and JavaScript Phrasebook 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.