1.8 JavaScript from External Files


When Web pages contain long scripts or functions that will be used by other documents, these scripts can be placed in external files. The external JavaScript files must end with a .js extension. The name of the external JavaScript file is assigned to the src attribute of the <script> tag in the HTML file. The name includes the full URL if the script is on another server, or just the script name if in the local directory. You can include more than one .js script in a file.

 
 <script language="JavaScript" src="http://servername/javascriptfile.js"> </script> 
Example 1.6
 <html>     <head><title>First JavaScript Sample</title> 1  <script language = "JavaScript" src="welcome.js">  2  </script>  </head>     </body> 3       <script language = "JavaScript"> 4            document.write("<body bgcolor='yellow' text='blue'>");              document.write("<font size='+3'>This is just plain old              HTML stuff.              </font>");         </script>     </body>     </html> 

EXPLANATION

  1. The JavaScript <script> tag's src attribute is assigned the name of a file (name must end in .js ) that contains JavaScript code. The file's name is welcome.js and it contains a JavaScript program of its own.

  2. The JavaScript program ends here.

  3. A new JavaScript program starts here.

  4. The document.write() method displays output in the browser window.



JavaScript by Example
JavaScript by Example (2nd Edition)
ISBN: 0137054890
EAN: 2147483647
Year: 2003
Pages: 150
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net