Books
in black and white
Main menu
Share a book About us Home
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 .. 83 84 85 86 87 88 < 89 > 90 91 92 93 94 95 .. 118 >> Next

The good news is that you find out as soon as you load your page and take a look at it that something is amiss — because your source code appears right there on the page! Check out the source shown in Listing 16-2 to see what I mean.
Listing 16-2: HTML Source Containing Misplaced Scripting Statements
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
function test(inputValue) {
alert("Wow, I sure do love JavaScript!” +
”\nHere's what the public is saying about JavaScript: ” +
inputValue) 1
</SCRIPT>
// The addNumbers() function is incorrectly defined
// below the closing </SCRIPT> tag.
(continued)
266 Part V: The Part of Tens________________________
Listing 16-2 (continued)
function addNumbers(numberOne, numberTwo) {
return numberOne + numberTwo
}
</HEAD>
When you execute the code in Listing 16-2, you don’t see a JavaScript error, but you do see the text of the addNumbers() function displayed on-screen. (You don’t see a JavaScript error because the JavaScript interpreter can’t access any statement outside the beginning and ending <SCRIPT> tags — unless that statement is a value for an event handler.)
Moving the </SCRIPT> tag just after the closing brace of the addNumbers() function fixes this script, causing the JavaScript interpreter to interpret the addNumbers() function as JavaScript code.
Anytime that you see your well-crafted JavaScript statements displayed in living color on your page, you can be pretty sure that the problem is that your statements are outside the bounds of the <SCRIPT> and </SCRIPT> tags. Move the statements back to where they belong and they should behave.
Nesting Quotes Incorrectly
Nesting single and double quotes together, like the following lines, is perfectly legitimate:
onClick=”alert('This is an example of nested quotes.')” onClick='alert("This is another example of nested quotes.”)'
Just make sure that you don’t nest double quotes inside double quotes, or single quotes inside single quotes, like this:
onClick=”alert("Oops! Incorrectly nested quotes generate a syntax error!')'
If you must include a mismatched quote, you can — as long as you escape the mismatched quote. Escaping a quote tells the JavaScript interpreter not to expect a matching quote. You escape a quote by preceding that quote with a backslash, like this:
onClick=”alert('This escaped quote doesn\'t cause a problem.')”
Chapter 16: Ten (Or So) Most Common JavaScript Mistakes 267
Treating Numbers as Strings
Humans tend not to make a big fuss over the difference between text and numbers — at least, not in most contexts. For example, when you write a sentence in English, you don’t need to do anything different to include a number. (Even if you write 333 of them!)
Numbers and text strings are two very different things to most programming languages, though, and that includes JavaScript. In JavaScript, trying to treat a number as a string, as shown in Listing 16-3, generates an error every time.
Listing 16-3: JavaScript Source Containing Statement That Treats a Number Like a String
<SCRIPT .........................
function testlt(inputValue) {
// The bold() method you see in the next line is associated // with the String object (not the Number object).
document.write(inputValue.bold()) document.close()
</SCRIPT>
}
<FORM NAME="myForm">
// The following onClick event handler // sends the number 2 to the testIt() function.
<INPUT TYPE="button" onClick='testIt(2)'>
ME="testButton" VALUE="test"
The problem occurs when the number 2 is passed from the definition of the onClick event handler to the testIt() function, which isn’t set up to handle numbers. If you look at the testIt() function, you can see that it’s taking whatever the input value is (in this case, the number 2) and trying to call the String object’s bold() method on it. And that ain’t flying. The only thing that you can call a string function on is a string, and 2 isn’t a string!
(If you’d like more information on what a string is, take a look at Chapter 3.)
Sometimes you are going to want to send a number to a function and have that function deal with it as a string. In these cases, all you need to do is add lines like the following to your function:
function testlt(inputValue) {
// Set up a temporary string variable. var aString = ”"
268 Part V: The Part of Tens
// Place the input value i nto the temporary
// strin g variable.
aSt ring += inputVal ue
// Call the bold() method on the string version of
// the i nputValue.
doc } ument .write(aStr ing.bol d())
Now you can send whatever value you like to the testIt() function, and testIt() behaves nicely!
Treating Strings as Numbers
The preceding section shows what happens when you treat numbers as strings. As you might guess, the reverse — treating strings as numbers — also causes grief in JavaScript. Let me explain by way of the code snippet shown in Listing 16-4.
Listing 16-4: JavaScript Source Containing Statement That Treats a String Like a Number
Previous << 1 .. 83 84 85 86 87 88 < 89 > 90 91 92 93 94 95 .. 118 >> Next