<SCRIPT SRC="..."> (HTML Tag Attribute)

The URL to access an insertable fragment of JavaScript contained in an include file.

Availability:

JavaScript - 1.1
JScript - 3.0
Internet Explorer - 4.0
Netscape - 3.0
HTML syntax:<SCRIPT SRC="..."></SCRIPT>
Argument list:...A URL to reach an includable .js file.

This provides a way to include JavaScript from an external file and share some common functionality among several pages. No data is transported from page to page apart from that which is in the included file as assignment statements, so this is not a means of maintaining state between pages.

Although this behaves in the same way as script that is in the HTML page itself, some people prefer not to call this inline code because it's held in a separate file. It depends whether you are referring to the location of the code when you use the term inline or whether you are talking about how it is executed; included script code is executed inline.

You can refer to javascript .js files on your local client-side hard disk as long as the page is not being requested from a web server. To access a local client file in a page coming from a web server would be to break the security regimes established to prevent client systems being hacked by intruders.

This is a really useful technique for debugging because you can build an entire library of object disassembly and diagnostic tools and include them in a script block which you can then eliminate when the code goes into production use.

A lot of the undocumented features of the browsers were uncovered in this way.

Warnings:

Example code:

   <SCRIPT SRC="include.js">
   </SCRIPT>
   ---------------------------------------------------------
   // This content goes into include.js
   function getBrowserType()
   {
      var myUserAgent;
      var myMajor;
      myUserAgent   = navigator.userAgent.toLowerCase();
      myMajor       = parseInt(navigator.appVersion);
      if( (myUserAgent.indexOf('mozilla')    != -1) &&
          (myUserAgent.indexOf('spoofer')    == -1) &&
          (myUserAgent.indexOf('compatible') == -1) &&
          (myUserAgent.indexOf('opera')      == -1) &&
          (myUserAgent.indexOf('webtv')      == -1)
         )
      {
         return "N";
      }
      if (myUserAgent.indexOf("msie") != -1)
      {
         return "msie";
      }
      return "other";
   }

See also:.js, <SCRIPT ARCHIVE="...">, <SCRIPT>, Adding JavaScript to HTML, File extensions, Inline script, MIME types, SCRIPT.src, Security policy

Cross-references:

Wrox Instant JavaScript - page 42