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

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 82 83 84 85 86 87 < 88 > 89 90 91 92 93 94 .. 118 >> Next

Typing-in-a-Hurry Errors
Spelling and capitalization errors easily take first prize for being the most common mistakes that all JavaScripters make, from the greenest beginner to the most highly seasoned veteran.
The JavaScript interpreter is a stickler for correct spelling: You simply can’t access an object, property, method, variable, or function unless you spell its name properly. For example, the second line of the following bit of code generates an error:
var identification = "ABC”;
alert(”The id number is ” + identificatoin);
The JavaScript interpreter is also case-sensitive, which means you can’t substitute uppercase letters for lowercase letters in object, property, method, variable, and function names. The following example generates an error because the correct name of the method is toLowerCase() (not TOLOWERCASE()):
alert("Broadcast network ID = "
+ identification.TOLOWERCASE());
To detect and correct these errors:
Be aware, as you write your JavaScript code, that consistency in spelling and capitalization is essential to bug-free statements.
Take advantage of any spell-checking utilities or point-and-click method name insertion utilities that your text editor provides.
JavaScript scripts are typically rife with pairs: pairs of opening and closing tags (courtesy of HTML), pairs of parentheses and curly braces, pairs of single quotes and double quotes. The JavaScript interpreter treats the stuff between the pairs as one entity, so if half of the pair is missing, the interpreter gets confused — mighty confused, in some cases!
The following are specific examples of happy couples that you don’t want to break up in JavaScript.
Looking at the following code, you’d think that the display would include two text elements: one to hold a first name and one to hold a last name. It doesn’t, though, because a closing angle bracket is missing.
<FORM NAME=”myForm”>
First name: <INPUT TYPE=”text” NAME=”firstName” LENGTH=15 Last name: <INPUT TYPE=”text” NAME=”lastName” LENGTH=30>
If a text element doesn’t appear — no error message, no nothing, just blank space where the element should have appeared — the likely suspect is a missing angle bracket on the line directly before the invisible text element.
The code that you see in Listing 16-1 depicts a tiny little script, perhaps a first attempt at a JavaScript-enabled Web page. At first blush, perhaps you don’t see anything amiss. If you were to load this script, though, you’d see that something is definitely amiss!
Listing 16-1: HTML Source Containing a Missing Tag
Lonely angle brackets
Lonely tags
<HEAD>
<SCRIPT ............................
function test() {
var aString = "some text”
(continued)
264 Part V: The Part of Tens
Listing 16-1 (continued)
alertC'aString is ” + aString)
}
//The closing //SCRIPT) tag that should be here is missing.
</HEAD>
/BODY)
/FORM NAME=”myForm”>
/INPUT TYPE=”button” NAME=”testButton” VALUE=”test” onClick='test()'>
<P>
First name: /INPUT TYPE=”text” NAME=”firstName” LENGTH=15>
Last name: /INPUT TYPE=”text” NAME=”lastName” LENGTH=30>
//FORM>
//BODY>
//HTML>
The absence of the closing / /SCRIPT> tag in the preceding code snippet causes the page to display nothing — zip, nada, zilch — instead of the button and text elements that you expect.
Whenever elements refuse to appear, check your HTML statements to see whether an opening half of a two-part tag, such as /TITLE>, /SCRIPT>, or /BODY>, is missing its closing half (//TITLE>, //SCRIPT>, and //BODY>, respectively).
Lonely parentheses
When you look closely at the body of the following test() function, you can easily spot the missing parenthesis on line three:
function test() {
var aString = "some text” alertC'aString is " + aString
}
As your JavaScript skills increase, though, you might find yourself putting together whopping long statements. Furthermore, each of the whopping long statements might contain many pairs of parentheses, often nested a few layers deep — and that’s when you’re most likely to make this kind of mistake.
Unless the editor that you use to create your script provides an automatic parentheses-pair-checking utility, you need to eyeball your code to catch and correct this mistake.
Chapter 16: Ten (Or So) Most Common JavaScript Mistakes 265
Lonely quotes
Take a good look at the following example:
<INPUT TYPE=”button” NAME=”testButton” VALUE=”test”
onClick='test("hello)'>
The mistake here is that no closing double quote appears after the word hello. The preceding code doesn’t generate an error; it just disables the
testButton object’s onClick event handler.
Here’s how the corrected statement looks:
<INPUT TYPE=”button” NAME=”testButton” VALUE=”test”
onClick='test(”hello”)'>
Putting Scripting Statements in the Wrong Places
When you’re new to JavaScript, remembering the order of things might be a little difficult. For example, JavaScript statements are valid only when they’re placed between the <SCRIPT> and </SCRIPT> tags or as values assigned to event handlers. If you forget and place them somewhere else, you’re bound to get an unexpected result.
Previous << 1 .. 82 83 84 85 86 87 < 88 > 89 90 91 92 93 94 .. 118 >> Next