CSCI 102T
The Socio-Techno Web
Home | Schedule | Labs | CS@Williams
Lab 6: Introduction to JavaScript
Background Info
Start by reading the selected pages of the Introduction
and Chapter 1
from the book JavaScript & jQuery: The Missing Manual (whole book)
by David Sawyer McFarland. It is highly recommended that you go back and read these chapters closely at a later
time. For now it is ok to read quickly and then move on to the programming parts of the lab.
As we have seen before, the Web is a great resource for learning more about JavaScript. If you want more info about
JavaScript, this tutorial should be helpful.
Exercise 1
<script>This code is HTML, not JavaScript. It tells the browser that the text following this tag will be JavaScript.
alert('hello world');You've just typed your first line of JavaScript code! The alert() function is a command that pops open an Alert box and displays the message that appears inside the parentheses. Notice the punctuation used (parenthesis, quotes, semicolons). This punctuation is very important. We'll discuss this in detail later.
</script>
Exercise 2
<script> document.write('<p>Hello world!</p>'); </script>Like the alert() function, document.write() is a JavaScript command that literally writes whatever you place between the opening and closing parentheses to the webpage containing the script. Note that using document.write() after an HTML document is fully loaded deletes all existing HTML. Thus document.write() is most often used for testing.
Exercise 3: Mad Libs
In this final exercise, we are going to use JavaScript prompts and variables to make our own Mad Libs game. (If you aren't familiar with Mad Libs, spend a few minutes learning about them.) In general, a user will be presented with windows, where they will input text, which will then be put into a premade story with some missing words. Here is an example.
<script language="JavaScript" type="text/javascript">
var adjective;"var" is used to define a variable, and as you may have guessed "adjective" is the variable’s name in this case. Variable names can only be used one time each in a script, meaning you cannot have two variables named "adjective." The exception to this rule is that variable names are CaSe SeNsItIvE, meaning capitalization matters, especially in calling the variable’s contents at a later time. You will probably want to use a variable to describe each value entered by the user.
adjective=prompt("Please enter an Adjective.", "");Take a moment to study the punctuation above. After prompt(), you need an opening ( followed by an opening ". After the quote, you put in what you want the user to see as a description for what to put in the box. After the message, you add a closing ". Next, you add in a comma, to show you’re moving to a new attribute, and put "" in order to leave the next field blank. You will then need to add a closing ) followed by a ;. The semicolon (;) is very important, as it lets the browser know to stop executing that command. Many syntax errors arise from the lack of a semicolon.
document.write("<p>There once was a " + adjective + " dog.</p>");Note that this is done preferably on one line. When you use the document.write() function, you will use parentheses with quotations, much like the prompt() command. Inside the quotes, you can put in the bulk of the story, but remember to put in spacing where it is needed. Between each piece of text add a "+" to add (or concatenate) it together. To call in a variable, you type the variable’s name.
Tracking Down Errors
The most frustrating moment in JavaScript programming comes when you try to view your JavaScript-powered page in a web browser and nothing happens. Most browsers silently ignore JavaScript errors. If you'd like to figure out what's wrong, there are ways to track errors. Most web browsers keep track of errors and record them in a separate window called an error console. In Firefox (on the Macs) go to Tools → Web Developer → Browser Console, and pick the "Errors" tab. In Chrome, use the keyboard shortcut Command + Option + J (on Mac) or Control + Shift + J (on Windows/Linux). You'll see many things here, including CSS errors in addition to JavaScript errors.
Tasks for lab