JavaScript Bible, Gold Edition part 214 docx

10 196 0
JavaScript Bible, Gold Edition part 214 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CD-622 Part VI ✦ Appendixes Listing 37-8 (continued) Enter a string to act as a delimiter between entries: <INPUT TYPE=”text” NAME=”delim” VALUE=”,” SIZE=5><P> <INPUT TYPE=”button” VALUE=”Array as-is” onClick=”showAsIs(this.form)”> <INPUT TYPE=”button” VALUE=”Reverse the array” onClick=”reverseIt(this.form)”> <INPUT TYPE=”reset”> <INPUT TYPE=”button” VALUE=”Reload” onClick=”self.location.reload()”> <TEXTAREA NAME=”output” ROWS=4 COLS=60> </TEXTAREA> </FORM> </BODY> </HTML> Notice that the solarSys.reverse() method stands by itself (meaning, nothing captures the returned value) because the method modifies the solarSys array. You then run the now inverted solarSys array through the array.join() method for your text display. array.sort([compareFunction]) NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓ ✓✓✓✓ Example You can look to Listing 37-9 for a few examples of sorting an array of string values. Four buttons summon different sorting routines, three of which invoke comparison functions. This listing sorts the planet array alphabetically (forward and backward) by the last character of the planet name and also by the length of the planet name. Each comparison function demonstrates different ways of comparing data sent dur- ing a sort. Listing 37-9: Array.sort() Possibilities <HTML> <HEAD> <TITLE>Array.sort()</TITLE> <SCRIPT LANGUAGE=”JavaScript1.1”> solarSys = new Array(9) array.sort() CD-623 Appendix F ✦ Examples from Parts III and IV solarSys[0] = “Mercury” solarSys[1] = “Venus” solarSys[2] = “Earth” solarSys[3] = “Mars” solarSys[4] = “Jupiter” solarSys[5] = “Saturn” solarSys[6] = “Uranus” solarSys[7] = “Neptune” solarSys[8] = “Pluto” // comparison functions function compare1(a,b) { // reverse alphabetical order if (a > b) {return -1} if (b > a) {return 1} return 0 } function compare2(a,b) { // last character of planet names var aComp = a.charAt(a.length - 1) var bComp = b.charAt(b.length - 1) if (aComp < bComp) {return -1} if (aComp > bComp) {return 1} return 0 } function compare3(a,b) { // length of planet names return a.length - b.length } // sort and display array function sortIt(form, compFunc) { var delimiter = “;” if (compFunc == null) { solarSys.sort() } else { solarSys.sort(compFunc) } // display results in field form.output.value = unescape(solarSys.join(delimiter)) } </SCRIPT> <BODY onLoad=”document.forms[0].output.value = unescape(solarSys.join(‘;’))”> <H2>Sorting array elements</H2> This document contains an array of planets in our solar system.<HR> <FORM> Click on a button to sort the array:<P> <INPUT TYPE=”button” VALUE=”Alphabetical A-Z” onClick=”sortIt(this.form)”> <INPUT TYPE=”button” VALUE=”Alphabetical Z-A” onClick=”sortIt(this.form,compare1)”> <INPUT TYPE=”button” VALUE=”Last Character” onClick=”sortIt(this.form,compare2)”> Continued array.sort() CD-624 Part VI ✦ Appendixes Listing 37-9 (continued) <INPUT TYPE=”button” VALUE=”Name Length” onClick=”sortIt(this.form,compare3)”> <INPUT TYPE=”button” VALUE=”Reload Original” onClick=”self.location.reload()”> <INPUT TYPE=”text” NAME=”output” SIZE=62> </TEXTAREA> </FORM> </BODY> </HTML> array.splice(startIndex , deleteCount[, item1[, item2[, itemN]]]) NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility ✓✓ ✓ Example Use The Evaluator (Chapter 13) to experiment with the splice() method. Begin by creating an array with a sequence of numbers: a = new Array(1,2,3,4,5) Next, remove the center three items, and replace them with one string item: a.splice(1, 3, “two/three/four”) The Results box shows a string version of the three-item array returned by the method. To view the current contents of the array, enter a into the top text box. To put the original numbers back into the array, swap the string item with three numeric items: a.splice(1, 1, 2, 3, 4) The method returns the single string, and the a array now has five items in it again. ✦✦✦ array.splice() Symbols </ /> comment tag, 41 <!—comment—> tag, 26 =+ (add-by-value operator), CD-90 <> delimiter characters, 4 != (does not equal comparison operator), CD-43 == (equals comparison operator), CD-43, CD-58 > (greater than comparison operator), CD-43 >= (greater than/equal to comparison operator), CD-43 < (less than comparison operator), CD-43 <= (less than/equal to comparison operator), CD-43 $1 $9 property, RegExp object, 1031 @ symbol, CD-1–CD-2 A A element object, 493–504 abbr property TD element object, 654 TH element object, 654 above property, layers, 860–861 acceptCharset property FORM element object, 537 FORM object, 537 accessKey property, 109–110 LABEL element object, 418–419 action property FORM element object, 537–538 FORM object, 5 activeElement property, document object, 344 ActiveX controls, 6 ActiveXObject object, 1140–1141 add-by-value operators, CD-90 addBehavior( ) method, 149–150 AddDesktopComponent( ) method, 237 addEventListener( ) method, 91–92, 151–153 AddFavorite( ) method, 237 addImport( ) method, styleSheet object, 790–791 addReadRequest( ) method, userProfile object, 706–707 addRule( ) method, styleSheet object, 791 Adobe Acrobat Reader, 6 alert( ) method, 255, CD-21, CD-90 alertUser( ) function, CD-30 align property APPLET element object, 903 EMBED element object, 914 HR element object, 416 IFRAME element object, 311–312 Image element object, 509 OBJECT element object, 908 TABLE element object, 630 alignment, IFRAME element object, 311–312 aLink property, BODY element object, 400–401 alinkColor property, document object, 344–345 all property, 110–111 simulator, 100–101 alt property APPLET element object, 903–904 AREA element object, 522 Image element object, 509–510 OBJECT element object, 909 altHTML property APPLET element object, 904 OBJECT element object, 909 altKey property, event object IE4, 747–748 NN6+, 764–765 altLeft property, event object (IE4), 748–749 Anchor element object, 493–504 Index 1474 Index ✦ A anchors property, document object, 345–346 animation, 6 APIs (application programming interfaces) compatibility and, 1266–1268 scripting and, 1200–1206 appCore property, 229 appendChild( ) method, 86, 153–154 APPLET element object, 902–906 applet-to-script application example, 1193–1197 applets, 7 control, 1178 data type conversion, 1189–1190 JSObject class, 1191–1193 limitations of, 1185 playing, 7 properties, 1180 scripting examples, 1181–1190 source code, 1186–1187 stopping/starting, 1180 text files, reading, 1188 applets property, document object, 346 applications calculations, 1355–1363 decision helper, 1375–1398 graphics, 1355–1363 intelligent updated flags, 1365–1373 lookup tables, 1299–1309 map puzzle, DHTML, 1399–1414 order forms, 1311–1319 table of contents, outline style, 1322–1353 XML data islands, transforming, 1415–1429 applyElement( ) method, 154–155 ARCHIVE attribute, signing scripts, 1248 archive property, APPLET element object, 904 AREA element object, 520–524 arg1 variable, 60 arguments. See also parameters functions, CD-52 methods, CD-20 arguments property, Function object, 1097 arithmetic operators, 1074–1076, CD-43 arity property, Function object, 1097 Array( ) function, CD-55 Array object methods, 998–1005 properties, 996–998 arrays, 987–988, CD-55–CD-59 attributes, 111 childNodes, 115 creating, 988–989, CD-55–CD-56 creation enhancements, 991 data access, CD-56 decrementing through, 130–131 deleting entries, 991–992 document objects in, CD-58–CD-59 document.all, 71 document.images, 51–52, CD-112 FORM object, 536 forms, 535–536 initializing, 988 literal notation, 991 multidimensional, 995–996 onImgArray, CD-113 parallel, 992–995, CD-56–CD-58 populating, options for, 989–991 used as stacks, 1000 assign( ) method, location object, 330 assignment operators, 1070, 1076–1078, CD-37 assignments, event handlers, 51 attachEvent( ) method, 155–156 attribute object, 112 properties, 167 attributes CONTENT, 48 deleting, 158–159, 179–180 HEIGHT, 124, CD-110 ID, 125, 1249–1250, CD-15 LANG, 129 LANGUAGE, 39, CD-23–CD-24 NAME, CD-15 SRC, 42–43, CD-24 TYPE, CD-24 WIDTH, 124, CD-110 1475 Index ✦ A–B attributes array, 111 attributes property, 111–113 audio, 6 aural properties currentStyle object, 839–840 runtimeStyle object, 839–840 style object, 839–840 authoring environment setup, 20–23 MacOS and, 22 overview, 11–13 Windows and, 21 work-flow, 20–21 WYSIWYG tools, 19 autocomplete property, FORM element object, 538 availHeight property, screen object, 699–700 availLeft property, screen object, 701 availTop property, screen object, 701 availWidth property, screen object, 699–700 axis property TD element object, 654 TH element object, 654 B back( ) method history object, 335 window object, 256 back-end programs, 5 background properties currentStyle object, 821–826 runtimeStyle object, 824–826 style object, 821–826 background property BODY element object, 401 TABLE element object, 631 BASE element object, 475–477 BASEFONT element object, 477–478 BaseHref property, OBJECT element object, 909 batch mode validation, 1151 BBEdit (Bare Bones Software), text editor, 20 behavior components event handlers and, 1276 methods, 1276–1277 properties, 1276–1277 Behavioral Extensions for CSS, 1273 behaviorCookie property, event object (IE4), 749 behaviorPart property, event object (IE4), 749 behaviors components, linking, 1274–1275 components, structure, 1275–1277 enabling/disabling, 1275 examples, 1277–1283 Internet Explorer, 1273 style sheets and, 1273–1274 text objects, CD-78 behaviorUrns property, 113 below property, layers, 860–861 beta browsers, compatibility and, 54 bgColor property BODY element object, 400–402 document object, 344–345, 346 layers, 862 MARQUEE element object, 421 TABLE element object, 631 bgProperties property BODY element object, 402 bidirectional event model, 90–91 binding, data binding, 120–121 bitwise operators, 1070, 1082–1083 blank frames, 223–224 BLOCKQUOTE element object, 410 blur( ) method, 156–158 Text Input object, 578 BODY element object, 399–407 event handlers, 407 methods, 405–407 properties, 400–405 body property, document object, 347 <BODY> tag, onDragDrop event handler and, 293–294 1476 Index ✦ B–C body text objects, 409–472 bookmarks property, event object (IE4), 749–750 Boolean object, 965 Boolean operators, 1070, 1078–1082 Boolean values, CD-33, CD-36 border and edge properties currentStyle object, 826–832 runtimeStyle object, 826–832 style object, 826–832 BORDER attribute, 300 border property FRAMESET element object, 307 Image element object, 510 TABLE element object, 632 borderColor property FRAME element object, 300 FRAMESET element object, 307–308 TABLE element object, 632 borders color, 300, 308 frames, 302 size, 307 bottom property, TextRectangle object, 471–472 bottomMargin property, BODY element object, 402–403 boundElements property, event object (IE4), 749–750 boundingHeight property, TextRange object, 453 boundingLeft property, TextRange object, 453 boundingTop property, TextRange object, 453 boundingWidth property, TextRange object, 453 BR element object, 411 braces, curly, CD-54–CD-55 branching index pages, implementation, 47–49 branching variables, 95 break statement, CD-58 browser sniffer scripts, 48 browser wars, 12–13 browsers branching index pages, 48–49 crashes, debugging, 1235 document object, 63–64 inline branching, compatibility, 1262–1263 non-DHTML, 1268–1269 nonscriptable, 45–46 older, <script> tag and, CD-26–CD-27 selecting, 20 threads, freezing, 287 version detection, 44–53 bubbles property, event object (NN6+), 765–766 bufferDepth property, screen property, 701–702 built-in objects, 58 BUTTON element object, 550–555 event handlers, 554–555 methods, 554 properties, 552–553 button elements accessing, CD-80–CD-81 adding, CD-13–CD-14, CD-16–CD-17 Button object, CD-79 button property event object (IE4), 750–751 event object (NN6+), 766 buttons, event handlers and, CD-21 C calculations application, 1355–1363 Date object, 978–982 calendars, 1285–1286 call( ) method, Function object, 1100–1102 caller property, Function object, 1098–1099 calling functions from event handler, CD-52 cancelable property, event object (NN6+), 767 1477 Index ✦ C cancelBubble property, event object IE4, 751–752 NN6+, 766–767 canHaveChildren property, 113–114 canHaveHTML property, 114 CAPTION element object, 645 caption property, TABLE element object, 633 captureEvents( ) method, 70 document object, 378–380 window object, 256–258 capturing events, 279–280 cascading style sheets, 16, 74 case, strings, converting, CD-91 cellIndex property TD element object, 654 TH element object, 654–655 cellPadding property, TABLE element object, 633 cells, tables, 615–616 content modification, 616–619 populating, 615–616 cells property TABLE element object, 634 TR element object, 650 cellSpacing property, TABLE element object, 633 CGI (Common Gateway Interface) cookies, 348 prototyping, CD-8 serverless, CD-7 servers, CD-1–CD-2 character codes, key codes comparison, 208 characterSet property, document object, 348 charCode property, event object (NN6+), 767–738 charset property A element object, 498 Anchor element property, 498–499 document object, 347 Link element object, 481, 498 META element object, 485 Checkbox Input object, 555–559 event handlers, 559 methods, 559 properties, 557–558 checkbox object, CD-79–CD-80 checked property, CD-79–CD-80 preferences and, CD-80 checked property, CD-79–CD-80 Checkbox Input object, 558 Radio Input object, 561–562 child/child references, 220, CD-102–CD-103 child frames, CD-99–CD-101 child nodes, 123 child/parent references, 220, CD-102 childNodes property, 114–115 children property, 115–116 chrome, windows, 235, CD-62 cite property, 410 CLASS attribute, 116 classes, 1178 JSObject, 1191–1193 scripting directly, 1214–1215 CLASSID attribute, 276 classid property, OBJECT element object, 909–910 className property, 116 clear( ) method document object, 380 selection object, 443–444 clear property, BR element object, 411 clearAttributes( ) method, 158–159 clearInterval( ) method, 258 clearRequest( ) method, userProfile object, 707 clearTimeout( ) method, 258–259 click( ) method, 159 BUTTON element object, 554 Checkbox Input object, 559 Radio Input object, 564 clientHeight property, 116–117 clientInformation object (IE4+), 681–683 methods, 681–683 properties, 667–681 clientInformation property, 229 1478 Index ✦ C clientLeft property, 117 clientTop property, 117 clientWidth property, 116–117 clientX property, event object IE4, 752–754 NN6+, 768–769 clientY property, event object IE4, 752–754 NN6+, 768–769 clip property, layers, 862–864 clipboardData property, 229–230 cloneContents( ) method, Range object, 431–432 cloneNode( ) method, 160 cloneRange( ) method, Range object, 431–432 close( ) method, CD-64 document object, 380–381 window object, 259 closed property, 231 closeNewWindow( ) function, CD-64 Code Base Principles, Evaluator Sr. and, 56 code property APPLET element object, 904–905 OBJECT element object, 910 codebase principal, digital certificates and, 1246–1247 codeBase property APPLET element object, 905 OBJECT element object, 910–911 codeType property, OBJECT element object, 911 COL element objects, 646–648 collapse( ) method Range object, 432–433 TextRange object, 454–455 collapsed property, Range object, 429 collections, 71 color borders, 300, 308 layers, 858–859 color property BASEFONT element object, 478 FONT element object, 412–413 HR element object, 416 colorDepth property, screen object, 702 cols property FRAMESET element object, 308 TABLE element object, 634 TEXTAREA element object, 585–586 colSpan property TD element object, 655 TH element object, 655 columns, tables, 625–627 comments, 26, 1137–1138 debugging and, 1229 statements and, CD-27–CD-28 commonAncestorContainer property, Range object, 429–430 compact property, OL element object, 657 compareBoundaryPoints( ) method, Range object, 433–434 compareEndPoints( ) method, TextRange object, 455–456 comparison operators, 1069, 1070–1071, CD-43 compatibility, 13–16 authoring and, 12–13 beta browsers, 54 designing for, 53–57 language, objects and, 13–14 ratings charts, 56–57 compile( ) method, regular expression object, 1025–1026 complete property Image element object, 510–511 Image Input object, 566 componentFromPoint( ) method, 160–162 Components property, 229, 231 concatenation, 27, CD-90–CD-91 strings, 928–929 conditions, loops, CD-50 1479 Index ✦ C confirm dialog box, 260 confirm( ) method, 260–261 connubial operators, 1070, 1073–1076 console windows, Netscape, debugging, 1218–1219 const statement, 1138–1139 constructor property Function object, 1100 string object, 932–933 containment hierarchy, 65 elements, 73–74 containment versus inheritance, 222–223 contains( ) method, 162 content, properties, 101–102 CONTENT attribute, 48 content property currentStyle object, 813 META element object, 486 contentDocument property FRAME element object, 301 IFRAME element object, 312–313 contentEditable property, 117–118 control selections, 198 control structures, 59, 1033, CD-48–CD-49 conditional expressions, 1038–1039 if constructions, 1034–1038, CD-48–CD-49 if-else constructions, 1034–1038, CD-49 labeled statements, 1048–1050 for loops, 1039–1044 with statement, 1047–1048 switch statement, 1050–1053 controllers property, 229, 231 controls, form controls, CD-76 converting case, strings, CD-91 data types, CD-40–CD-42 data types, applets and, 1193 numbers to strings, CD-42 strings to numbers, CD-41–CD-42 cookie property, document object, 348–358 cookies access, 350 Bill Dortch’s cookie functions, 354–357 CGI and, 348 domain, 352 expiration dates, 351–352 files, 349 retrieving data, 352–353 saving, 351–352 SECURE parameter, 352 strings, parsing, 353 subcookies, 358 throwaway passwords, 350 value, assigning, 351 coordinates, scrolling and, 280–281 coords property A element object, 498–499 Anchor element object, 498–499 AREA element object, 523 Link element object, 498–499 Core DOM, 76–77 core language, 14–15 core language objects, CD-89 createAttribute( ) method, document object, 381–382 createCaption( ) method, TABLE element object, 639–340 createContextualFragment( ) method, Range object, 434–435 createControlRange( ) method, BODY element object, 405 createElement( ) method, 87 document object, 382–383 createEventObject( ) method, document object, 383–384 createPopup( ) method, 261 createRange( ) method, selection object, 444 createStyleSheet( ) method, document object, 384 createTextNode( ) method, 87 document object, 385 createTextRange( ) method, BODY element object, 405 createTextRange( ) method, TEXTAREA element object, 586–587 . Possibilities <HTML> <HEAD> <TITLE>Array.sort()</TITLE> <SCRIPT LANGUAGE= JavaScript1 .1”> solarSys = new Array(9) array.sort() CD-623 Appendix F ✦ Examples from Parts III and IV solarSys[0] = “Mercury” solarSys[1]. CD-622 Part VI ✦ Appendixes Listing 37-8 (continued) Enter a string to act as a delimiter between entries: <INPUT. TYPE=”button” VALUE=”Last Character” onClick=”sortIt(this.form,compare2)”> Continued array.sort() CD-624 Part VI ✦ Appendixes Listing 37-9 (continued) <INPUT TYPE=”button” VALUE=”Name Length” onClick=”sortIt(this.form,compare3)”> <INPUT

Ngày đăng: 06/07/2014, 06:20

Tài liệu cùng người dùng

Tài liệu liên quan