Share this Page URL

Chapter 17. The Programmable Page: Addin... > Using the <SCRIPT> Tag - Pg. 194

The Programmable Page: Adding JavaScripts to Your Pages 194 · If you need to adjust the script, you have to adjust it only in the separate file. Every page that accesses the script automatically uses the edited version. · It makes your page's source code look less cluttered, so it's easier to read. Webmaster Wisdom This section on using an external JavaScript file is advanced stuff, so don't worry if it makes no sense to you now. Just skip over it and come back to it later, after you've had some experience working with JavaScripts. The key thing about all this is that you can put only what are known as functions in the external file. A JavaScript function looks like this: function Name() { A bunch of programming statements go here. } Here, Name is the name of the function. To tell the browser about the external file, add the SRC attribute to the <SCRIPT> tag. For example, if your functions are all in a file named scripts.js (it's traditional to use the .js extension for these files; they're just text files, however), you'd put the following into your page between the <HEAD> and </HEAD> tags: <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="scripts.js"> </SCRIPT> Some JavaScript Examples The real purpose of this chapter isn't so much to show you how to insert scripts (although that's clearly important). No, what I really want to do is give you a good supply of scripts to use, at no extra charge, in your own pages. To that end, the next few sections take you through quite a few Java- Scripts that perform all manner of interesting and useful functions. (Remember, too, that you get even more script examples in the next chapter.) Displaying a Message to the User Let's begin with the simplest of all JavaScript functions: displaying a message to the user in a simple dialog box (also called a pop-up box or an alert box ). Here's the JavaScript code that does the job: alert("Insert your message here") In other words, between the quotation marks you enter whatever message you want to display. For example, suppose you want to have a message pop up each time a user visits your home page. You can do that by inserting the following script between the </HEAD> and <BODY> tags (see jsalert1.htm on the CD in this book):