15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Parsing methods<br />

string.charAt(index)<br />

Chapter 16 ✦ String and Number Objects (Chapters 34 and 35)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Enter each of the following statements into the top text box of The Evaluator:<br />

a = “banana daiquiri”<br />

a.charAt(0)<br />

a.charAt(5)<br />

a.charAt(6)<br />

a.charAt(20)<br />

Results from each of the charAt() methods should be b, a (the third “a” in<br />

“banana”), a space character, and an empty string, respectively.<br />

string.charCodeAt([index])<br />

String.fromCharCode(num1 [, num2 [, ...<br />

numn]])<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Listing 34-2 provides examples of both methods on one page. Moreover, because<br />

one of the demonstrations relies on the automatic capture of selected text on the<br />

page, the scripts include code to accommodate the different handling of selection<br />

events and capture of the selected text in Navigator and Internet Explorer 4.<br />

After you load the page, select part of the body text anywhere on the page. If you<br />

start the selection with the lowercase letter “a,” the character code displays as 97.<br />

If you select no text, the result is NaN.<br />

Try entering numeric values in the three fields at the bottom of the page. Values<br />

below 32 are ASCII control characters that most fonts represent as hollow squares.<br />

But try all other values to see what you get. Notice that the script passes all three<br />

values as a group to the String.fromCharCode() method, and the result is a combined<br />

string. Thus, Figure 16-1 shows what happens when you enter the uppercase<br />

ASCII values for a three-letter animal name.<br />

471<br />

stringObject.charCodeAt()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!