You can add scripts to your document in two primary ways. One is to place the script into the head portion of your document. This is referred to as an embedded script. The other way is to place your script external to the document, which is referred to as a linked script.
Embedding a Script
Example 3-9. Embedding a script into the head of a document
The purpose of this script is to set up the document to open the image photo.jpg in a new window when a specific link is clicked. You also need a bit of script in the actual link found in the body of the document, as follows:
Figure 3-12 demonstrates how clicking on the link makes the pop-up window appear with the image intact.
Figure 3-12 The results of the embedded script.
Linking to a Script
In terms of best practices, the more you can get out of your document and into external files in terms of scripting and style, the better. You can have many pages pointing to one script, and if you require changes to the script, you can make them to the one script file instead of to many documents with embedded scripts.
To link to the script, first place the script code (without any HTML) into a separate file, and name the file with a .js extension, as in popup.js. You can place that file into a subdirectory named scripts (just as you did with images and media), and then use the script element to link it to the document (see Example 3-10).
Example 3-10. Linking to the script
Leave the link code as is within the body of the document, and the results will be exactly the same as demonstrated in Figure 3-12.