in black and white
Main menu
Home About us Share a book
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Javascript for dummies. Quick Reference - Vander

Vander Javascript for dummies. Quick Reference - Wiley Publishing, 2002. - 115 p.
Download (direct link): javascriptquickreference2002.pdf
Previous << 1 .. 14 15 16 17 18 19 < 20 > 21 22 23 24 25 26 .. 72 >> Next

Nested quotes
You use quotes in JavaScript — both single quotes (') and double quotes (") — to surround string values. Why both kinds of quotes? Because you may run into a situation where you need to use two sets of quotes in a single JavaScript statement. If so, you need to use both single and double quotes and alternate them.
Basic Punctuation and Syntax
If you try to nest double quotes inside double quotes (or single quotes inside single quotes), you run into trouble. Here’s an example:
onClick="al ert('This is an example of correctly nested quotes . ')" onClick="alert("Warning! This statement will produce an error.")" onClick='alert('Warning! This statement is wrong, too.')'
If you want a double quote to appear in a string, here’s what you do: Precede the double quote with a backslash. (This action is called escaping the quote.) Here’s what it looks like:
alertC'Did you like the movie \"Phenomenon\"?")
JavaScript scripts are typically rife with pairs — pairs of opening and closing tags and angle brackets (courtesy of HTML), pairs of parentheses, pairs of quotes, and pairs of curly braces. If you forget to add a closing bracket, brace, or whatever, the JavaScript interpreter complains. Sometimes the complaint takes the form of a syntax error; sometimes you get a goofy-looking page display.
Following are some examples of pair mismatching to look out for:
HTML/JaVaScript Statement Error
<F0RM NAME="myForm" Missing right angle bracket (»
<A HREF="http://www.tucows. com">Two Cows Missing closing tag (</A>)
alertC'Form processing complete." Missing parenthesis ())
fi rstName = '‘Barney Missing quote (")
if (name == "") { alert("PI ease enter your name.") Missing curly brace (})
Spelling and capitalization (case)
All the words you use in programming JavaScript must be spelled correctly. For example, if you create a variable called 1 astName and then try to display it on your Web page but misspell it as 1 astNam, you get an error. As close as these two words may appear to human eyes, they look nothing alike to the JavaScript interpreter.
Comments (I*.. .*I and II)
Character case is just as important as correct spelling. For example, the JavaScript interpreter won’t recognize the variable named 1 astName if you type it LastName.
The JavaScript interpreter reads from top to bottom, left to right. So, before you can use something, that something must first be defined. Case in point: In order to call (or use) a function, you must first define that function in an earlier statement. Likewise, if you want to access a variable, you must declare that variable first.
Comments (/*.. *1 and II)
Comments aren’t processed at all by the JavaScript interpreter; they’re ignored. A comment’s purpose is to give script authors a free-form way to communicate with themselves (you’d be surprised how quickly you forget why you did something the way you did it!) and any other humans who read their scripts.
Two different kinds of JavaScript comments exist. Either can appear anywhere in your script, as many times as you want.
You create a single-line comment by typing a double slash (/ /) at the beginning of the line, followed by your comment, like this:
// This is a single-line comment.
Create a multiple-line comment by beginning a line with / * and ending your comment with */, like so:
/* This comment can span multiple lines. Always remember to close it off, though; if you forget, you'll get weird errors when you try to display your script. */
Nesting multiple-line comments is a bad idea. A block of code like the following can cause grief because the interpreter ignores the second / * when it gets to the first * /:
/* Blocking out this section for testing purposes . . .
/* Here is a comment. */
Conditional Expressions: if... else
The if. . .else expression is called a conditional expression because you use it to test whether a certain condition is true. A condition can be a variable, a statement, or an expression — anything at all that can be resolved by the JavaScript interpreter to a simple true or false answer.
If the condition is true, the interpreter executes all the statements between curly braces that follow the i f clause. If the condition is false, the interpreter executes all the statements between curly braces that follow the else clause. Here’s the generic description of how to use if. . .else:
if (condition) { statements
[ else { statements
The square brackets around the else clause mean that the else clause is optional — it’s possible to code just the i f clause, if you want. And no rule says that an i f. . . el se expression can’t have other statements nested inside of it, either (many do). Just remember to include the curly braces as shown for each if. . .else. There’s no leeway here; they have to be curly braces, not parentheses, and they have to come in pairs, just like in the following example:
if (numberOrdered <= 100) { //calculate the order at retail cost calculateTotal(19.95)
Previous << 1 .. 14 15 16 17 18 19 < 20 > 21 22 23 24 25 26 .. 72 >> Next