Attaching Scripts

The alternative to embedding is attaching, where you put the JavaScript in a separate file. However, for a Web page to use the external script, you have to tell the browser where to find it.

To do this, you use the src attribute of the script tag, like so:

<html>
  <head>
    <title>Page Title</title>
    <script language="JavaScript" src="../scripts/myscript.js">
    </script>
  </head>
  <body>
    <!-- The content of the page goes here. -->
  </body>
</html>

The src attribute gives the path from the current page to the script file that you want to attach, just like a hyperlink.

Get Web Design Garage 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.