Introduction to JavaScript

Presented at ~conf July 14, 2003 by Andrew Hedges, Design Team Manager, Interactive Multimedia Applications Group

How to insert JavaScript in a Web page

There are two ways to insert JavaScript in a Web page: inline or linked. Inline means the script is embedded in the page, like in the example below. Typically, scripts are put in the <head> of the page, but they can also be put in the <body>.

	<html>
	<head>
	   <title>My Page</title>
	   <script type="text/javascript">
	   
	   // my script can go here
	   
	   </script>
	</head>
	<body>
	
	<script type="text/javascript">
	
	// or my script can go here
	
	</script>
	
	</body>
	</html>

You can also link to a script in an external file. This is useful when you have a script that will be reused on multiple pages (like a DHTML menus script). Linking to scripts has the following advantages:

  1. You only have one copy of the script to maintain if you need to make changes to it.
  2. The browser software can cache the script so it only has to be downloaded once.

The code to link to a script is shown below.

	<html>
	<head>
	   <title>My Page</title>
	   <script type="text/javascript" src="scripts/myscript.js"></script>
	</head>
	<body>
	
	<!-- my page -->
	
	</body>
	</html>

Document URL: http://www.gwu.edu/~ahedges/javascript/
Copyright © 2003, The George Washington University. All rights reserved.