Please note that index links point to page beginnings from the print edition. Locations are approximate in e-readers, and you may need to page down one or more times after clicking a link to get to the indexed material.
! operator
bitwise OR, 128
NOT operator, 133
!= operator, 131
“ “ (quotation marks), 91–92
$ (dollar sign)
ECMAScript specifications for, 917
indicating position with, 272
-- (decrement)operator, 130
% (modulus) operators, 123, 124
& (bitwise AND) operator, 128
&& (AND) operations, 133, 143–144
‘ ‘ (single quotation marks), 91–92
( ) (parentheses) operator, 139
* (asterisk)
indicating multiplication operator with, 123, 124
using as repetition quantifier, 274–275
+ operator
string concatenation with, 124–126
unary operators and type conversion using, 126
uses for, 104
++ operator, 130
, (comma)
separating variables with, 42
– (subtraction) operators
negating values with, 126
. (dot) operator
accessing properties with, 186
object access using, 47–48, 49, 137
: (colon), 184
; (semicolon)
separating statements with, 41–42, 120–121, 157–158
terminating statements with, 120–121
/ (forward slash)
defining regular expression escape codes with, 268–269, 272
/* */ (multiline) comments, 73, 858
= (assignment) operators
comparison vs., 131
types of, 55–56
using, 123
== (comparison) operator, 41, 95
assignment vs., 131
comparing objects with, 194
comparing strings, 133
effect on performance, 908
types of, 131–132
=== operator
comparing objects using, 131, 194
runtime optimizations with, 908
strict equality operator, 58, 94, 95, 132
>> operator, 128
>= operator, 131
>operator, 131
<= operator, 131
< operator, 131
<< operator, 128
<<< operator, 128
? (question mark), 295
?: operators, 134–135
(?: ), 294
(?= ), 294–295
(?! ) syntax, 295
[ ] (brackets)
defining array literals with, 51, 98, 137
defining character classes with, 276–278
using array operator, 188–189
\ (backslash), 272
^ (caret)
indicating bitwise XOR operator, 128
indicating position with, 272
|| (OR) operations, 133, 143–144
{ } (curly braces)
defining objects in, 98
enclosing object literals in, 184–185
using in statement blocks, 42, 121–122
|
performance limitations using, 296
searching for alternative patterns with, 279–280, 296
A
abort() method, 683
abstraction, 215
accordion widget
graceful degradation in, 621–623
progressive enhancement in, 616–621
ActiveX controls, 845–849
about, 845
embedding objects with, 846–847
implementing XHRs from browser using, 654
JavaScript interactions with, 847–849
plug-ins vs., 832
supporting XMLHttpRequest in legacy systems, 658–659
triggering security dialog in, 658
using wrapper functions with, 659–660
add() method, 574
addColorStop() method, 797, 798, 800
addEventListener() method, 444–445
addition (+) operator
string concatenation with, 124–126
unary operators and type conversion using, 126
adoptNode method, 380–381
advisory messages, 585–587
Ajax (Asynchronous JavaScript and XML), 647–713. See also XMLHttpRequest object about, 647
asynchronous XHR requests in, 662–663
authentication with XHRs, 684–686
binary bridge approach in, 693, 694, 703, 704
browser support for XHR objects, 658–660
challenges in, 713
Comet communication patterns in, 702–705
communicating data with XHR vs. <img>
tag, 698–702
communication flow using, 32–33, 647–649
controlling XHR requests, 683–684
cross-domain requests in, 692–695
form serialization, 690–692
“Hello World” example in, 649–653
HTTP HEAD method with, 666
JSON with, 676–681
managing MIME types, 686–687
onload, onloadstart, and onloadend events, 687–688, 696
onprogress event handler support by browsers, 689–690
POST requests for sending data, 665–666
response and responseType property, 674–675
response headers for XHRs, 682
responseText property in, 671
responseXML property, 671–674
returning JavaScript responses with JSON, 681
same-origin checks in requests from, 887–888
<script> tag in data communications, 701–702
sending data via GET requests, 664–665
setting request headers, 667–668
status and statusText properties in, 670–671
testing readyState values, 668–670
try-catch blocks to create XHR object, 650
understanding XMLHttpRequest object, 654–657
uploading files in, 691–692
using multipart responses, 687
XHR instantiation, 657–658
XHR requests, 660–662
alert() method, 480–481
alternative patterns, 279–280, 296
AND (&) operator, 128
AND (&&) operations, 143–144
animation
controlling with DHTML, CSS, and JavaScript, 782–788
implementing with setTimeout(), 784, 785
redrawing object in different position, 820–822
requestAnimationFrame() technique, 821–822, 824
SVG for, 823–824
anonymous functions, 171–172
appendChild() method, 359
applets
ActiveX vs. Java, 845
controlling in JavaScript, 850–853
JavaScript access by, 853
using within Java <object> tag, 849–850
applications. See also Flash; Web applications
Ajax communication flow in, 648–649
caching site resources offline, 763–765
HTTP authentication using XHRs, 684–686
traditional communication flow in, 647, 648
apply() method, 177
arcs
arc() method, 793
drawing, 793–800
arguments
Date() constructor, 247
passing to functions, 99–100
arithmetic operators
combining bitwise, assignment, and, 129–130
array ([ ]) operator, 188–189
array literals, 218
Array.isArray() method, 232
accessing elements of, 218–219
adding and changing elements in, 219–220
Array.isArray() method for, 232
associative, 309
characteristics as reference type, 191
concat() method for, 225–226
creating with array literals, 218
declaring, 217
defining, 98–99
detecting, 100–101
drop-in form validation using, 594–596
ECMAScript 5 extensions to, 75, 231–245
every() method, 234–237
extending with prototypes, 224, 230–231
filter() method, 240–241
forEach() method, 238–239
indexOf() method, 232–233
JSON, 677
lastIndexOf(), 233–234
length property for, 221–223
map() method for, 239–240
methods for, 225–229
multidimensional, 230
passing arguments to constructors, 217
placing parsed cookies in associative, 752–753
printing content of mimeTypes[ ], 834, 835
reduce() method, 241–243
reduceRight() method, 243–245
refreshing plugins[ ], 839–840
removing elements in, 187, 220
reverse() method, 226–227
slice() method, 227
some() method for, 237–238
sort() method, 228–229
splice() method, 227–228
toString() and toSource() methods for, 228
treating as stacks and queues, 223–224
using array operator, 188–189
assignment (=) operators
combining bitwise, arithmetic, and, 129–130
types of, 55–56
using, 123
associative arrays, 309, 752–753
associativity of operators, 59–60, 139–141
asterisk (*)
indicating multiplication operator with, 123, 124
using as repetition quantifier, 274–275
async attribute, 17
Asynchronous JavaScript and XML. See Ajax
asynchronous requests for XHR objects, 662–663
attachEvent() method, 434–436
attributes. See also name attribute
associating HTML elements with id, 307, 308
async, 17
autofocus, 584
browser support for data-*, 385–386
case in DOM-compliant names, 376
checkbox and radio button, 568
command element, 624–625
creating window with tag’s event handler, 490
data validation with data-*, 597–599
defer, 765–766
dirname, 609–610
draggable, 631–632
executing inline scripts with async, 17
<form> tag, 550–551
HTML5 event handler, 12–13
making attribute nodes, 376–380
manipulating DOM, 375–376
matching name and id, 308
novalidate, 608
placing pluginspage in <embed>, 839
required, 604
retrieving HTML elements by name, 344–345
script element’s language, 9
setting pattern, 605
src, 13–15
title, 606
triggering validation, 605
type, 9–10
value, 568
<audio> tag
media object properties for, 829–830
methods of Media object, 831
authentication with XHRs, 684–686
autofocus attribute, 584
automatic error reporting, 874–875
B
Back button, 522
beginPath() method, 794–796
beveled lines, 793
bezierCurveTo() method, 793, 794
binary bridge approach (Ajax), 693, 694, 704
bitmaps images. See also client-side graphics
inserting in drawings, 806–808
working with client-side, 788–791
bitwise operators
combining arithmetic, assignment, and, 129–130
shift operators, 128–129
truth tables for, 127
blocks, curly braces in, 42, 121–122
<body> events, 424–425
BOM (Browser Object Model)
about, 30
early evolution of, 319–323
illustrated, 298
bookmarklets, 17
Boolean built-in objects, 245–246
Boolean data types
constructing, 94–95
converting other types to, 103
defined, 43
brackets ([ ])
defining array literals with, 51, 98, 137
defining character classes with, 276–278
using array operator, 188–189
break statement, 63–64, 152–154
breakpoints, 871
browser detection
checking for user language supported, 738
checking online status, 738–739
confirming Java support by, 724–725
defined, 715
determining network connection with script timer, 739–740
finding if JavaScript supported, 721–722
geolocation, 734–737
identifying Screen object’s characteristics, 726
information used for, 719–720
JavaScript version detection by, 722–723
JavaScript’s, 718–719
markup and style detection, 720–721
media queries aiding, 730–734
Navigation Timing API and window.
performance object, 740–742
relying on JavaScript object detection, 723–724
technical issues for, 720–725
testing for plug-in support, 725
visual detection methods, 725–730
browser development tools, 897
Browser Object Model. See BOM
browser safe palette, 729–730
browser sniffing, 27
browser state events, 468–470
browsers. See also browser detection;
cookies;
state;
and specific browsers
ActiveX controls in Internet Explorer, 832, 845–849
ActiveX XHR fallback in Internet Explorer, 658–659
adding mouse events in, 453–456
avoiding history leaks in, 304
beyond DHTML object models, 323–324
Canvas API support and differences, 822
canvas element support in, 788
case in DOM-compliant attribute names, 376
challenges supporting Date object, 251
checking if online, 738–739
comparing RegExp properties for
compatibility with JavaScript push and pop
methods, 224
content handler methods for Navigator
object, 748
controlling appearance of, 742–748
CSS and JavaScript rollovers used in, 775–780
custom context menus for, 627–631
data-* attributes support in, 385–386
dealing with JavaScript implementations, 26–28
debuggers in, 871–872
deleting cookies, 753–754
detecting, 715
determining network connection with
script timer, 739–740
developer tools in, 897
developing wrappers for Internet Explorer
event handlers, 314–315
development of DHTML-oriented object
models for, 320–322
differences with RegExp object properties, 287, 289
direct path styles for older, 346
displaying screen properties for, 725–730
DOM Traversal API support in, 410–412
ECMAScript array extensions for, 75, 231–245
ensuring page compatibility with, 846–847
geolocation used by, 734–737
handling multipart responses, 687
hash changes in older, 521
identifying Screen object’s
characteristics, 726
implementing Find dialog for, 510
inconsistencies with reserved words, 915–917
indicating type with Navigator object, 716–719
information for detecting, 719–720
insertion methods extending DOM, 360–362
instantiating object properties for, 305–306
Internet Explorer’s handling of plug-ins, 839
interpreting <script> tag for JavaScript, 8–12
Java support detected by, 724–725
JavaScript pseudo-URL bookmarklets stored in, 17–19
JavaScript’s interactions with plug-ins, 839
JScript compatibility with Internet Explorer, 24–26
key/value pairs stored in, 758–760
markup and style detection by, 720–721
media queries used by, 730–734
MIME types support detected by, 833–837
mouse wheel events supported by, 456–458
Navigation Timing API and window. performance object, 740–742
Netscape/Mozilla/Firefox versions of JavaScript, 23–24
one-time pop-up windows in, 755
onload, onloadstart, and onloadend event support in, 687–688
onprogress event handler support by, 689–690
page redirection using cookies, 754–755
patching array callback function for, 237–238
plug-ins used in, 832
polyfills for noncompliant, 576
pop-up blocker detection by, 503–505
profilers optimizing JavaScript, 902, 908
<progress> tag support detected by, 720–721
protocol handlers, 746–747
providing information for Event object, 416
refreshing plugins[ ] array, 839–840
relying on JavaScript object detection, 723–724
resolveURL() method unimplemented in, 519
saving state values to user’s disk, 525
script execution order for, 765–766
search bars built into, 744–746
security issues for cookies, 754
setting background color on type of, 719
setting cookies, 750–751
simulating browser button presses, 522, 743–744
state management in, 749
status bar for, 525–527
storing client-side data with IndexedDB API, 760–763
supporting modifier y in regular expressions, 269
technical issues for detection, 720–725
testing for plug-in support, 725
turning off validation by, 608
turning on error information in, 861–863
types of events in, 416
unreliable compatibility with DOM, 328–330
user language supported by, 738
using window object for, 475–476
version detection for JavaScript by, 722–723
visual detection methods, 725–730
ways to detect JavaScript support by, 721–722
window.print() support for, 509
without JavaScript support, 20–22
XHR object wrappers for, 659–660
XMLHttpRequest object support by, 654
bubbling events about bubbling phase, 443
adding bubble phase handler for DOM events, 444–445
creating custom, 465–467
event delegation for, 906–907
Internet Explorer, 439–440
preventing, 440
built-in objects Array, 217–245
Boolean, 245–246
Date, 246–253
JavaScript, 217
Math, 256–258
Number, 258
String, 259–265
butt line cap property, 792
Button object, 561
buttons. See also radio buttons
about HTML, 557–558
coding within <form> tag, 558
creating with <button> tag, 559–560
loading page by clicking, 517–518
rollover, 775
simulating browser button presses, 522, 743–744
triggering creation of windows, 488
C
caching data for performance, 902
call() method, 176–177
call stack, 870–871
callback function, 237–238
caller property, 175–176
calling frame, 287
camel-back naming convention, 39
Canvas API. See also drawing animation for, 820–822
browser support and differences for, 822
capturing events, 814–818
compositing options for, 812–813
customizing drawing with transformations, 802–806
hit testing for, 818
inserting bitmaps in drawings, 806–808
modifying canvas with toDataUrl(), 820
redrawing, 818–820
saving state for, 814
using transform matrix for paths, 804–806
canvas element adding shadows in, 809, 810
calling drawImage() method on, 807
creating arcs and curves for, 793–800
CSS color functions for, 790
drawing lines and shapes on, 792–793
examples using, 790–791
inserting <canvas> tag in page, 788–789
laying out multiple drawings on canvas, 812–813
Path API methods for, 796–797
properties and methods for, 789–790
redrawing images on, 818–820
saving changes to, 820
Text API methods and properties for, 811
text support for, 808–812
use of, 788
using perspective with, 800–802
<canvas> tag, 788–790
capture phase, 443
indicating bitwise XOR operator, 128
indicating position with, 272
Cascading Style Sheets. See CSS
case-sensitivity capitalization of variables, 108–109
case in DOM-compliant names, 376
event handlers, 425
importance in HTML, XHTML, and XML, 356
insensitivity of event handlers, 13
Internet Explorer’s searches for id values and, 335
JavaScript, 38–39
unspecified case for positional keywords, 368
catching. See also try/catch blocks
exceptions, 876
form fill-in errors, 589–591
centering windows, 502–503
characters. See also special characters classes of, 276–278
commonly used character classes, 278–279
examining string, 259–260
grouping in patterns, 275–276
JavaScript support for character sets, 90–91
layered encoding of, 91
negative character classes, 278
strings and special, 90
whitespace, 40
checkboxes
HTML syntax for, 567
properties for DOM Checkbox object, 568
checked attribute, 568
checkValidity() method, 551
child nodes
illustrated, 334
inserting tag before, 360
normalizing appended, 358–360
classes
character, 276–279
defining class properties, 204
documenting, 861
dynamic styling using CSS, 403–407
negative character, 278
click-to-edit interfaces, 639–643
client-side graphics, 788–791
adding gradient to canvas in, 798–799
applying perspective, 800–802
creating arcs and curves, 793–800
drawing lines and shapes, 792–793
examples using, canvas element, 790–791
inserting bitmaps in drawing, 806–808
inserting <canvas> tag in page, 788–789
linear gradients, 797–798, 799, 800
properties and methods for <canvas>
object, 789–790
scaling drawings, 802–803
using transform matrix for paths, 804–806
vector graphics with SVG, 822–824
client-side servers Client Capabilities feature of Internet Explorer, 740
client-side data validation, 31, 597, 610
data validation on server-side vs., 609
security for JavaScript on, 883–886
storing client-side browser data in databases, 760–763
using JavaScript cross domain requests, 682–685
clientInformation object, 716
cloneNode (deep) method, 370
close() method, 489–490
closures
about, 168–169
wrapping function calls in, 662
coding. See also writing JavaScript
buttons within <form> tag, 558
execution order, 38
style for, 856–858
timers, 528–530
collections
DOME object images[ ], 345–346
dynamic styling using CSS, 403–407
finding with getElementsByClassName()
method, 349–353
radio button, 569–570
colon (:), 184
color adding linear gradient, 797–798
handling conditional uses of, 729
properties and methods for canvas element, 800
setting browser background, 719
color pickers, 576–577
command element, setting attributes of, 624–625
comparing strings, 133
comparing undefined types with, 95
effect on performance, 908
types of, 131–132
comparisons
type conversion in, 58
compile() method for RegExp objects, 282
composite data types, 96–101
arrays, 51–52
defined, 47
ECMAScript built-in and host objects, 50–51
functions as data types, 52–53
primitive data types vs., 96
concat() method, 225–226
concatenating
arrays, 225–226
conditional operators, 56
confirm() method, 481–483
console messages, 864
constants
Math object, 256
unsupported by JavaScript, 117–118
constructors
arguments to Date(), 247
converting to native objects, 200–201
CustomEvent(), 467
Date(), 246–247
defining functions for, 97–98
object instance creation with, 183–184, 199–200
passing array arguments to, 217
referencing for prototypes, 206–208
RegExp (), 269
content handler methods, 748
context menus, 625–631
contexts
defined, 112
variable execution, 116–117
continue statement, 63–64, 152–154, 155
cookies
customizing pages with, 755–757
defined, 749
deleting, 753–754
enabling one-time pop-up windows, 755
ensuring safety of JavaScript, 893
HTTP-only, 893
limitations of, 758
persistent, 749
protecting from unauthorized changes, 754
reading and parsing, 751–753
setting in JavaScript, 749–751
stolen by XSS embedded script, 891–892
syntax and tokens for, 749, 750
Coordinates object, 735
copying nodes, 370
createPopup() method, 485–487
cross-domain requests
Internet Explorer XDR object for, 695–698
using Ajax in, 692–695
Cross-Site Request Forgery (CSRF), 893–895
cross-site scripts (XSS)
HTTP-only cookies preventing cookie stealing, 893
potential risks of, 891–892
security for, 892–893
CSRF (Cross-Site Request Forgery), 893–895
CSS (Cascading Style Sheets)
accessing complex DOM style rules, 408–410
animation using DHTML, JavaScript, and, 782–788
applying form button styles with, 560
applying to HTML image buttons, 558–559
color functions for canvas element, 790
combining with DHTML, 782
controlling images with, 788
displaying styles used in DOM, 407–408
dynamic DOM styling of classes and collections, 403–407
errors styling objects sharing ids with, 345
handling relative text sizing in, 729
making media queries, 730–734
mapping CSS2 to DOM properties, 397–400
menu element options when rendering with JavaScript and, 624–625
properties and methods for canvas element styles, 800
selectors for HTML5 form validation, 607
style detection by browsers, 720–721
style object properties, 408
styling DOM with style property, 397–403
using style sheets with DOM, 396
values for media queries, 732–733
curly braces ({ })
defining objects in, 98
enclosing object literals in, 184–185
using in statement blocks, 42, 121–122
curves
bezierCurveTo() method, 793, 794
drawing, 793–800
quadraticCurveTo() method, 793, 794
custom context menus, 627–631
custom events, 465–468
customizing web pages with cookies, 755–757
D
data
caching for performance, 902
communicating with <script> tag, 701–702
consuming response data in JSON, 680–681
limiting during typing, 603–604
POST requests for sending, 665–666
sending via GET requests, 664–665
setting MIME type for returned, 686–687
transporting with JSON, 676–678
data-* attributes, 385–386, 597–599
data lists, 587
data types. See also variables
composite, 96–101
functions as, 52–53
methods for converting, 45–47
number, 84–89
reference, 191
representing data accurately, 88–89
return values for typeof operators, 136–137
strings, 89–94
type conversion for, 44–45, 101–106
Date object, 895–896
date pickers, 574–576
dates, 246–253
about Date object, 246
converting to strings, 249–250
creating, 246–247
limitations of Date object, 251
manipulating, 248–249
new ECMAScript additions for, 251–253
turning strings into, 250–251
using four-digit year in, 246
dead code, 905
debugging JavaScript output manually, 869–870
JavaScript with stack traces, 870–871
using browser debuggers, 871–872
declaring
arrays, 217
regular expressions, 269
decrementing operand with -- operator, 130
deep clones, 370
default behaviors
DOM, 448–449
event handlers, 429–430
default methods and properties, 210
default values in prototypes, 202–203
defensive programming, 873–878
art of, 873–874
automatic error reporting for sites, 874–875
exception management, 876
try/catch syntax for exceptions, 877–878
using onerror handler of window object in, 874
defer attributes, 765–766
delete operator, 138
deleting
cookies, 753–754
menu options using remove(), 574
nodes, 370–372
destruction of JavaScript objects, 185–186
detachEvent() method, 434–436
DHTML (Dynamic HTML)
components of, 782
confusion introduced with, 31
controlling animation using JavaScript, 782–788
DHTML event model, 432–434
DHTML-oriented object models, 320–323, 324
fundamentals of, 31–32
dialogs, 480–487
alert, 480–481
confirmation, 481–483
creating with window.print(), 509
defined, 480
implementing Find, 510
Internet Explorer error, 864
modal, 484–485
modeless window, 485
overlays, 505–508
pop-up, 485–487
Print, 509
prompt, 483–484
triggering security dialog in ActiveX, 658
types of, 480
differential inheritance, 206–208
direct path styles, 346
dirname attribute, 609–610
disabled form fields, 587–588
disabling greedy matching, 295
display property, 644
displaying script errors, 6
div-based dialogs, 505–508
<div> blocks, hidden, 407–408
division(division) operators, 123, 124
do-while loops, 150–151
DOCTYPE statements, 539
Document Object Model. See DOM
Document objects about, 31, 96–97, 301
access by name, 307–308
accessing by position, 306–307
associative arrays for accessing, 309
common starting points for tree walks, 348–349
DOM methods for accessing, 309–311
examples of properties for, 303–305
getElementsById() method, 335
images[ ] collection of, 771
lowest common denominator properties for, 302
methods for, 303
document trees. See also nodes
accessing elements of, 334–336
case for tag elements in, 356
common starting points for traversing, 348–349
manipulating with DOM, 331–334
skipping elements with traversal properties, 343
text nodes for, 335–336
walking known tree structure, 336–341
documenting code, 858–861, 862
document.referrer attribute, 301
documents adding text with insertAdjacentHTML()
and insertAdjacentText() methods, 367–368
DOM document trees, 331–334
placing <script> element in <head> tag, 11–12
sharing nodes between, 380–382
using <script> elements in, 10–11
dollar sign ($)
ECMAScript specifications for, 917
indicating position with, 272
DOM (Document Object Model), 327–414
accessing complex style rules, 408–410
accessing document objects using, 309–311
accessing document tree elements, 334–336
addEventListener() and removeEventListener() methods in, 444–445
appending and inserting nodes, 358
binding event handlers to elements for, 416, 443
browser handling of DOM tree variations, 341–342
building HTML editor using DOM methods, 392–394
canceling default actions for events, 448–449
continuing evolution of, 413
controlling event propagation, 449–450
copying nodes in, 370
creating nodes, 356–357
CSS style sheets with, 396
displaying styles used in, 407–408
document objects for, 182
document.write() and document.writeln()
methods, 368–369
DOM Traversal API, 410–412
dynamic styling of classes and collections, 403–407
event creation in, 451–453
event model for, 313, 415, 432–434, 443–453
extending and inserting after node objects, 360–362
finding collections for, 349–353
getElementsByName() method, 344–345
HTML element mappings, 388–393
illustrated, 298
importNode and adoptNode methods for, 380–381
inline style manipulation for, 397–403
innerText and outerText properties in, 366–367
insertAdjacentHTML() and insertAdjacentText() methods for, 367–368
insertBefore() method for, 360
inserting code with innerHTML property, 363–365
invoking event handlers, 316
isNodeEqual() and isNodeSame() comparisons for, 381–382
list of mouse events, 453
making attribute nodes, 376–380
manipulating attributes, 375–376
manipulating document trees with, 331–334
mapping HTML elements to, 384–387
methods supporting namespaces in, 382–383
minimizing bottlenecks accessing, 905–906
modifying text nodes, 373–375
mousewheel events, 456–458
need for dynamic markup in, 362–363
node properties available for, 335–336
node types for, 333
object collections for, 345–346
object model components used in, 316–319
properties of Event object, 446–448
querySelector() and querySelectorAll() selector methods, 353–356
representing <input> tags as HTMLInputElement objects, 555
selecting content ranges with DOM Range API, 412–413
selection schemes for, 311–312, 321, 322
Storage object, 758–760
table manipulation in, 394–396
tree traversal starting points for, 348–349
UI events, 458–459
unreliable browser compatibility with, 328–330
using in C/C++ and Java, 328
W3C level definitions for, 327–328
walking known tree structure in, 336–341
working with outerHTML property, 365–366
DOM Element Traversal Specification, 342–343
DOM Range API, 412–413
DOM Traversal API, 410–412
DOM trees. See document trees
DOMContentLoaded events, 426, 469–470
dot (.) operator accessing properties with, 186
object access using, 47–48, 49, 137
drag-and-drop functionality
adding to user interface elements, 631–639
drag-and-drop uploading, 636–638
event handlers for, 632–633
draggable attribute in HTML5, 631–632
drawImage() method, 806–807
drawing
adding text to Canvas API, 808–812
arcs and curves, 793–800
images with perspective, 800–802
inserting bitmap images in, 806–808
lines and shapes, 792–793
rotating bitmap images, 803, 806
shapes with SVG, 823
smiley face, 794–796
using transform matrix for paths, 804–806
drawShape() function, 817–818
drop-in form validation, 594–596
dropzone attribute in HTML5, 632, 639
Dynamic HTML. See DHTML
dynamically scoped properties, 287
E
address validation for, 296, 591–592, 596–597
protocol handlers for custom, 746–747
ECMAScript
built-in and host objects for, 50–51
changes in version 5, 73–76
continuing changes in, 76
dates in version 5, 251–253
defined, 30
extensions to arrays in version 5, 75, 231–245
function.prototype.bind() for, 75
ISO dates in, 75
native array additions in, 75
native JSON support, 75
new features for objects in version 5, 210–215
properties of objects in version 5, 76
reserved keywords for version 3, 106
reserved words in versions 3 and 5, 913–915
revised specifications for $ character, 917
string changes in version 5, 264–265
trimming whitespace from String objects, 76
using strict mode for functions, 175
versions of JavaScript, 22–23
editing array elements, 219–220
embedding
ActiveX in Web pages, 845–846
content for plug-ins, 833
JavaScript in case insensitive HTML, 39
<embed> tag
HTML5 specifications for, 833
including pluginspage attribute in, 839
encapsulation, 215
encoding
JavaScript security with, 885–886
layered character, 91
UTF-8 character, 671
encrypting JavaScript, 885–886
Error() constructor, 876
error messages. See also Error object; errors
attributes triggering validation, 605
during form validation, 599–602
examples of, 864
turning on, 861–863
Error object. See also error messages; errors properties of, 876–877
using instance in try/catch blocks, 877–878
errors. See also exceptions
catching form fill-in, 589–591
combining pre- and post-increment/
decrement operators, 131
creating with variable dash separators, 109
displaying script, 6
exception management, 876
keyboard masking to limit data entry, 603–604
reporting automatically, 874–875
semantic, 866–869
syntax, 865
using markup with JavaScript, 4–8
whitespace and syntax, 120
working with Error object properties, 876–877
escape codes
character class, 278–279
defined, 90
list of, 91
using for quotes, 91–92
using in regular expressions, 272–273
event binding
binding event handlers to DOM elements, 416, 428, 443
JavaScript attributes for, 425–428
nonstandard Internet Explorer, 420
support in HTML5, 417
ways to use, 416
event handlers, 415–471. See also event binding; Internet Explorer event model; traditional
event model
accessing as method of objects, 425
binding to DOM elements, 416, 428, 443
bypassing onsubmit handler with submit()
method, 551
canceling default actions for DOM, 448–449
case insensitivity of, 13
controlling DOM event propagation, 449–450
creating window within attribute of tag’s, 490
developing wrappers for Internet Explorer, 314–315
direct assignment of, 313
DOM event creation, 451–453
drag-and-drop, 632–633
firing events manually, 430–432
inline, 312–313
invoking, 316
issues with event models, 471
keyboard events, 460–462
nonstandard events, 464
onkeypress, 603–604
onprogress, 689–690
overview of, 415–417
properties of DOM Event object, 446–448
return values’ effect on default behaviors, 429–430
scope of, 428–429
setting with HTML event attributes, 533
text events, 462–464
triggering JavaScript, 12–13, 14
using onerror handler of window object, 874
validating forms with onchange event handlers, 602–603
variable scope and, 114–116
WebSocket, 712
XDR, 696
event listeners
addEventListener() and removeEventListener() methods, 444–445
using for DOMContentLoaded events, 426
event models. See also events
about JavaScript, 417
defined, 415
Internet Explorer’s, 434–442
issues with, 471
overview of modern, 432–434
traditional, 417–432
Event object
data contained in, 416
Internet Explorer’s properties for, 436–439
properties of DOM, 446–448
events. See also bubbling events; event binding; event handlers; event listeners
adding and removing using DOM methods, 444–445
Ajax-style applications and user, 648
application cache, 765
<body> and <frameset>, 424–425
browser state and loading, 468–470
calculating pixels on onmouseover, 815–816
capture phase of, 443
capturing canvas, 814–818
core HTML4, 418–421
custom, 465–468
defined, 415
delegating for runtime performance, 906–907
DOMContentLoaded, 469–470
event creation in Internet Explorer model, 441–442
firing manually, 430–432
focus, 459–460
hooking up to SVG, 823–824
HTML5 core, 421–424
HTML5 drag, 633
keyboard, 460–462
Media object, 831–832
mouse, 453–458
naming, 416
nonstandard, 464
on/offline, 470
onbeforeunload, 468–469
onload, onloadstart, and onloadend, 687–688, 696
onreadystatechange, 469
overview of, 415–417
overwriting existing values of, 426
preventing bubbling, 440
proprietary window events added to window object, 534
setting event handlers in HTML, 533
setting event listeners for, 313–314
simulating event routing, 441
text, 462–464
triggering, 416
types of, 312
UI, 458–459
used in Comet-style communication pattern, 703
WebSocket, 712
window, 530–534
exceptions
managing, 876
same-origin policy, 886–888
try/catch syntax for, 877–878
writing, 72
exec() method for RegExp objects, 282–286
execution contexts for variables, 116–117
execution order
browser script, 765–766
within code, 38
explicit type conversion, 105–106
expressions. See also regular expressions
about, 157
about JavaScript, 53
arithmetic operators, 53–54
creating regular, 71
defined, 38
evaluating with switch statements, 145–148
short-circuiting evaluation of logical, 143–145
extended object model, 320
extending native object prototypes, 203–204
F
fields. See also select menus adding labels for field selection, 584–585
advisory and status messages for, 585–587
attributes for accessing, 552–554, 555
checkboxes, 567–569
common properties on Form Field objects, 556
disabled and read-only, 587–588
file upload, 578–579
focusing first, 584
hidden form, 582
HTML5 date pickers, 574–576
masking entry, 603–604
properties for text and password, 561
radio button, 567, 568, 569–570
search fields and spin boxes, 564
select menus, 570–574
selecting multiple values on, 579
semantic text, 563–565
sliders in HTML5, 577–578
text areas, 565–567
traditional text, 560–563
types of HTML elements used as, 554–555
validating forms with hidden, 596–599
<fieldset> tag, 583
FIFO (first-in, first-out) order, 223
File API, 579–582
file upload fields, 578–579
files. See also uploading files
controlling SWF, 842–843
Gzip compression of, 901
including information at top of JavaScript, 860
minification of, 900–901
packaging scripts into, 901
picking multiple, 580–582
transferring with drag-and-drop lists, 635–639
fill, 798
filter() method, 240–241
find() method, 510
Firefox
DHTML object model evolution in, 324
flags altering regular expressions, 269
modifying XHR support in, 659
mousewheel events, 456–458
recommended frame properties in, 540
rendering windows in Internet Explorer vs., 726–730
saving state information in, 525
setting window height in, 493
support for sticky instance property, 286
first field focus, 584
first-in, first-out (FIFO) order, 223
flags
adding to exec() method of RegExp objects, 283–286
altering regular expression interpretation, 269
multiline, 293–294
Flash
banned from Apple iOS devices, 853
controlling plug-in with JavaScript, 840–845
Flash-based sockets, 712
testing support for, 725
triggering with ActiveX controls, 847–849
Flash player, 649
floating-point values, 84, 257
flow control
if statements for, 60–61
labels for break and continue statements, 153–154, 155
loop, 63–64
focus events, 459–460
fonts, 809
for-in loops, 156–157, 189–190
for loops, 151–152
forEach() method, 238–239
form fields. See fields
Form object
methods for, 551
properties of, 550–551
sample rendering of, 553–554, 555
form validation
about, 549–550
CSS selectors for HTML5, 607
drop-in, 594–596
error messages during, 599–602
future trends in JavaScript, 909
HTML5 properties and methods for, 606–607
JavaScript for, 549
limiting data during typing, 603–604
novalidate attribute, 608
overview of, 589–591
properties of ValidityState object, 606, 607
regular expressions for, 591–594
setting options in hidden fields, 596–599
validating forms with onchange event handlers, 602–603
forms, 549–610. See also fields; form validation
about, 549–550
accessing fields and, 552–554, 555
advisory and status messages in, 585–587
checkbox fields, 567–569
color pickers, 576–577
common properties on Form Field objects, 556
data lists in, 587
date pickers for, 574–576
disabled and read-only fields, 587–588
file upload fields, 578–579
focusing first field in, 584
generalized buttons on, 559–560
hidden form fields, 582
HTML elements used in, 554–555
HTML5 file handling modifications for, 579–582
image buttons for, 558–559
input element properties for, 555–557
internationalization of, 609–610
labels, fieldsets, and legends for, 583
labels for field selection, 584–585
overview, 610
performing form serialization with XHR, 690–692
picking multiple files from, 579–582
properties of Form object, 550–551
radio button fields, 567, 568, 569–570
search fields and spin boxes, 564
select menus, 570–574
semantic text fields, 563–565
sliders in HTML5, 577–578
Submit button attributes for, 608–609
text area fields, 565–567
traditional text fields in, 560–563
usability of, 583–588
validating, 549, 589–591, 604–606
forward references to functions, 160
frame busting, 545
frames, 538–548
accessing by name, 540
frame busting, 545
inline, 543–544
loading, 544–545
names and relationships between, 538–543
state management in, 546–548
windows and, 538
<frameset> events, 424–425
frozen objects, 213
full-screen windows, 501
function keyword, 159, 170–171
functions
anonymous, 171–172
arguments for, 99–100
avoiding excessive calls, 903–904
binding to instance context, 75
caller property for, 175–176
closures in, 168–169
commas vs. comma operators, 135
defining and calling, 160
defining for constructors, 97–98
defining property’s get or set, 212
function literals, 170–171
global and local variables in, 164–165
improving parameter handling for, 174–177
invoking for arrays, 238–239
limiting variable scope to particular, 167–168
mapping to element of array, 239–240
methods vs., 100
parameter passing by, 160–161, 173–174
passing object reference types to, 192–194
passing primitive data types, 162–164
passing to all array elements, 234–237
prototype property of, 201
recursive, 177–179
return statements with, 161–162
strict mode for, 175
syntax of, 68–70
treating as objects, 169–177
using as data types, 52–53
using static variables, 172–173
future reserved words, 914
future trends
expanded use of JavaScript, 909
in JavaScript, 908–911
media management, 853
G
garbage collection in JavaScript, 185–186, 907
GC (Garbage Collector), 907
generalized buttons, 557, 559–560
geolocation, 734–737
GET requests, 664–665
getAllResponseHeaders() method, 682
getCurrentPosition() method, 735, 736
getElementById() method, 335
getElementsByClassName() method, 349–353
getElementsByName() method, 344–345
getElementsByTagName() method, 347–348
getPrototypeOf() function, 208
getResponseHeader() method, 682
getTime() function, 895–896
Global object, 253–256
global variables
defining and naming, 110
functions using, 164–165
scope of, 112
globalAlpha property, 812, 813
globalCompositeOperation property, 812, 813
graceful degradation
approaching accordion widget with, 621–623
gradients
adding color stop to, 797, 798, 800
linear, 797–798
transparency in linear, 797–798
graphics. See client-side graphics; images
greedy matching, 295
grouping characters in patterns, 275–276
Gzip compression of files, 901
H
hash values in URLs, 520–522
<head> tag
linking script in document head, 15–16
placing <script> element in, 11–12
height
detecting and resizing browser window size, 726–730
image button, 559
“Hello World” example, 649–653
code for, 653
HTTP transactions in, 652
JavaScript, 3–8
testing URL in browser, 650–651
triggering request in, 650
try-catch blocks to create XHR object, 650
hexadecimal literals, 84–85
hidden <div> blocks, 407–408
hidden form fields
about, 582
validation using, 596–599
hiding JavaScript, 910
history hack, 304
History objects
about, 301
pushstate() and replacestate() methods of, 522, 523–525
hit testing for Canvas API, 818
host objects, 182
HTML (Hypertext Markup Language). See also specific versions of HTML
advantages of div dialogs, 505–508
applying CSS to image buttons, 558–559
associating id attribute with elements of, 307, 308
binding event handlers in markup, 416
<button> tag syntax in, 559–560
case in DOM-compliant attribute names, 376
creating window within tag’s event handler attribute, 490
elements corresponding to JavaScript Document object, 306
embedding JavaScript in case insensitive, 39
event handler attributes for elements in, 12–13
including SVG document in, 822–823
marking up strings as, 262–264
properties of Button object, 561
representing <input> tags as DOM HTMLInputElement objects, 555
syntax for checkboxes and radio buttons, 567
using buttons in forms, 557–558
core events for, 418–421
DOM-element mappings for, 388–393
elements used in forms, 554–555
mapping HTML elements to DOM, 384–387
HTML5
canvas element API features in, 792–793
color pickers, 576–577
confusion introduced with, 31
core events for, 421–424
data lists, 587
date pickers, 574–576
detecting support for <progress> tag, 720–721
DOM-element mappings for, 388–393
DOM specification and, 328
draggable attribute in, 631–632
elements used in forms, 554–555
<embed> tag specifications in, 833
event binding support in, 417
event handler attributes for elements in, 12–13
form validation properties and methods, 606–607
improved form validation for, 604–606
including <noscript> element in, 21
JavaScript client-side Web applications and, 34
JavaScript Form object and <form> tags, 550–551
mapping HTML elements to DOM, 384–387
menus and context menus elements in, 623–631
Navigator object content handler methods, 748
onhashchange events in, 521–522
passing data between windows in, 536–538
picking multiple files with File API, 579–582
revealing content on demand, 644
semantic text fields, 563–565
sliders in, 577–578
Submit button attributes for, 608–609
unspecified case for positional keywords in, 368
using History object’s pushstate() and replacestate() methods, 522, 523–525
using polyfills for noncompliant browsers, 576
validation properties and methods, 606
<video> tag for, 825–830
WebSocket communications in, 709–712
window events in, 530–533
HTMLButtonElement object, 560, 561
HTMLFormElement object, 550
HTMLInputElement objects, 555
HTMLMediaElement object, 826–827
HTMLOptionElement object, 573
HTMLSelectElement object, 570, 571
HTMLSourceElement object, 825
HTTP (Hypertext Transfer Protocol)
making HEAD requests in Ajax communications, 666
POST requests for sending data, 665–666
sending data via GET requests, 664–665
as stateless protocol, 749
transactions in “Hello World” example, 652
HTTP Basic Authentication, using, 684–686
HTTP-only cookies, 893
Hypertext Markup Language. See HTML
Hypertext Transfer Protocol. See HTTP
I
I/O (input-output) in JavaScript, 64–68
icons
appearing in alert dialog, 481
Internet Explorer error, 861–863
id attribute
accessing form with, 552
associating with HTML elements, 307, 308
associative arrays with, 309
identifiers
if statements
basic syntax of, 141–143
flow control using, 60–61
short-circuit evaluation of, 143–145
switch statements vs., 146, 148
<iframe> tag
sandboxing and iframes, 890–891
uploading files using, 691–692
Image object, 772–774
ImageData API, 808
images, 771–781. See also client-side graphics
accessing properties of Image object, 772–774
applying perspective to bitmap, 800–802
compositing options for Canvas API, 812–813
DHTML-style animation of, 782–788
handling conditional uses of, 729
JavaScript and CSS rollovers, 775–780
light boxes for, 780–782
loading, 806
properties and event handlers for Image object, 771–772
redrawing on canvas, 818–820
translating bitmap, 803–804, 806
working with client-side bitmap graphics, 788–791
images[ ] collection of Document object, 771
<img> tag
creating buttons with, 558–559
Image object properties and attributes of, 771
referencing image in inactive state with, 775
transmitting data with, 698–702
implicit semicolon insertion, 121
implicit variable declaration, 111
importNode method, 380–381
increment/decrement operators, 130–131
incrementing operand with ++ operator, 130
indeterminate value for checkbox fields, 568
indexedDB API, 760–763
indexOf() method, 232–233, 260
Infinity constant
defining with Global object, 253
inheritance
creating basic, 49–50
creating with Object.create() pattern, 209–210
developing custom errors using, 876
differential, 206–208
OOP characteristic of, 215
via prototypes, 205–206
initiating mouse events, 455–456
inline frames, 543–544
inner functions
closures, 168–169
invoking, 167–168
innerHTML property, 253, 360, 363–365
innerText property, 366–367
<input> tag
creating file uploads with, 578–579
field properties for, 555–557
using for checkboxes and radio buttons, 567–568
insertAdjacentHTML() method, 367–368
insertAdjacentText() method, 367–368
insertBefore() method, 360
inserting code dynamically
after node objects, 360–362
appending and inserting nodes, 358
with innerHTML property, 363–365
innerText and outerText properties for, 366–367
insertAdjacentHTML() and insertAdjacentText() methods, 367–368
need for dynamic markup for, 362–363
instance properties of RegExp objects, 286–289
instanceof operator, 207–208
instantiating
object properties for browsers, 305–306
XMLHttpRequest objects, 657–658
internationalization of forms, 609–610
Internet Explorer. See also Internet Explorer event model
Active X controls in, 832, 845–849
ActiveX and XHR support in, 658–659
Canvas API support and differences, 822
clicking error icon in, 861–863
Client Capabilities features, 740
DHTML-oriented object models, 322–323, 324
DOM in later versions, 323–324
event model and events in, 432–434
example of error dialog in, 864
focus() method in, 509
nonstandard event binding within markup for, 420
plug-ins handling in, 839
proprietary event handlers for, 416
rendering windows in Firefox vs., 726–730
searching for id values in older versions, 335
support for DOM Traversal API, 410–412
traditional object models in, 319–320
using XDR object for cross-domain requests, 695–698
Internet Explorer event model, 434–442
attachEvent() and detachEvent() methods for, 434–436
event bubbling, 439–440
event creation in, 441–442
Event object of, 436–439
overview of, 432–434
preventing bubbling, 440
simulating event routing, 441
interval syntax, 528
interwindow communications
communicating among windows, 534–536
passing data between windows, 536–538
isContentHandlerRegistered() method, 748
isFinite() method, 254
isInteger() function, 593
isNaN() method, 254
isNodeEqual() method, 381–382
isNodeSame() method, 381–382
ISO 8601 date format, 75
items
finding in arrays, 232–233
searching array for, 233–234
J
Java
accessing applets from JavaScript, 850–853
applets in, 849–850
confirming browser detection for, 724–725
detecting with JavaScript, 850
DOM’s use in, 328
JavaScript vs., 849
javaEnabled() method, 724, 850
JavaScript. See also object-oriented JavaScript
features; runtime performance; writing JavaScript
accessing with Java applets, 853
adding in page’s user interface, 611–616
adding to XHTML documents, 8–19
Ajax communication flow using, 32–33
animation using DHTML, CSS, and, 782–788
approach to object-oriented programming, 197–210
binding event handler attributes with, 425–428
browser version history for, 23–26
combining with DHTML, 782
common errors writing, 867–868
common uses of, 31–34
constants unsupported by, 117–118
controlling Java applets in, 850–853
designating with language attribute, 9
detecting browser support for, 26–28, 721–722
detecting Java applets from, 850
ECMAScript versions for, 22–23
efforts to fix, 909–910
encoding and encrypting, 885–886
errors using markup with, 4–8
event handlers triggering, 12–13, 14
form validation in, 549
handling other scripting variables, 28–29
Hello World example in, 3–8
history of, 30–31
improving form usability, 583–588
including <noscript> element, 20–22
invoking with pseudo-URL, 17–19
Java vs., 849
limitations controlling images in pure CSS, 788
linked scripts using <src>, 13–15
logarithm computation in, 258
media query access via, 733–734
menu element options when rendering with CSS and, 624–625
monkey patching, 26
object models in, 297–299, 319–325
object-oriented features of, 49–50
performance considerations for, 895–908
programming terms for, 37–38
returning responses to browser using JSON, 681
rollovers in CSS vs., 778–780
same-origin policy for, 886–888
script masking, 19–20
<script> element uses in, 3–4
setting cookies in, 749–751
simulating asynchronous processing in, 767–768
support for character sets, 90–91
transmitting data with <img> tag, 698–702
trigonometric methods in, 257
using object model in, 316–319
VBScript used with, 849
JavaScript Object Notation. See JSON
JavaScript profilers, 902, 908
jQuery library, 880–883
JScript
about, 24
history and compatibility with Internet Explorer versions, 24–26
nonstandard event binding for, 420
JSDoc comment tags, 859–860
JSON (JavaScript Object Notation)
arrays in, 677
consuming response data in, 680–681
defined, 676
objects in, 677–678
parse() method for, 680
returning JavaScript responses with, 681
stringify() method for, 678–680
strings in, 676–677
K
key/value pairs, 758–760
keyboard events, 460–462
keywords
canvas element’s compositing operation, 812
defined, 38
unspecified case for positional, 368
L
<label> tag
adding for field selection, 584–585
using, 583
labeled breaks, 153–154
labeled continue, 153–154, 155
language attribute
designating JavaScript with, 9
indicating browser with, 738
JavaScript version detection with, 722–723
last-in, first-out (LIFO) order, 223
layered character encoding, 91
lazy-loading techniques, 899–900
<legend> tag, 583
length property
characteristics of array, 221–223
defining number of parameters accepted by function, 173
lexical scoping, 112
lexicographic comparisons, 58–59, 133
libraries
characteristics of JavaScript, 879–880
example using jQuery, 880–883
fixing JavaScript with, 909–910
susceptibility to XMLHttpRequest object hijacking, 890
using JavaScript, 34–35
LIFO (last-in, first-out) order, 223
light boxes, 780–782
linear gradients adding color to, 797–798, 799
lines
drawing, 792–793
lineJoin property, 793
lineWidth property, 793
linked scripts
pros and cons of, 16–17
using <src> with, 13–15
lists
data, 587
drag-and-drop, 635–639
scrolled, 570
literals
defined, 38
function, 170–171
hexadecimal, 84–85
object, 184–185
octal, 85
regular expression, 53
reserved, 914
LiveConnect, 850
LiveScript, 849
loading
events, 468–470
frames, 544–545
images, 806
linked scripts, 16
web pages with button click, 517–518
loading scripts, 898–900
bootstrapping, 899
dynamic script loading, 898–899
lazy-loading techniques, 899–900
overview of, 898
preloading techniques for, 900
functions using, 164–165
scope of, 112
Location object
accessing and setting window location with, 516–520
methods of, 518
logarithm computation, 258
logical expressions, 143–145
logical operators, 56, 133–134
lookahead expressions, 294–295
loop unrolling, 904
loops
cautions setting within interval, 529
controlling with continue and break statements, 152–153
do-while, 150–151
flow control for, 63–64
for, 151–152
for-in, 189–190
listing multiple statements in curly braces, 42
looping over radio groups, 569–570
performance and, 904
reducing use of, 904
RegExp word-parsing, 284–285
while, 148–150
writing, 61–62
M
mapping
CSS property and DOM objects, 397
CSS2 to DOM properties, 397–400
HTML element, 388–393
between HTML elements and DOM, 384–387
markup and style detection by browsers, 720–721
mask out, defined, 166
match() method for String objects, 292–293
Math built-in object
constants for, 256
methods for, 257
random number generation for, 258
media management
Active X for, 845–849
client-side graphics, 788–824
DHTML and animation, 782–788
future of, 853
HTML audio, 830–831
HTML video, 825–830
image handling, 771–781
Java applets for, 849–853
types of media events, 831–832
using plug-ins, 832–845
Media object
audio and video properties of, 829–830
events of, 831–832
methods of, 831
media queries
CSS values for, 732–733
examples of, 731
using for mobile technology, 733–734
memory management, 907
menu element, 624–625
menus
adding submenus, 630–631
detecting and controlling menu bars, 497–498
options when rendering with CSS and JavaScript, 624–625
pull-down, 570–572
scripting select, 573–574
showing custom context, 627–631
visibility of, 498
<meta> tag
name attribute for, 308
specifying script language with, 10
methods. See also specific methods
accessing Document objects with DOM, 309–311
addEventListener(), 444–445
adding and deleting menu options using add() and remove(), 574
alert(), 480–481
arc(), 793
array, 225–229
beginPath(), 794–796
blur(), 509
blur() and focus() for text fields, 562
building HTML editor using DOM, 392–394
<canvas> tag, 789–790
close(), 489–490
common object, 196–197
comparisons using isNodeEqual() and isNodeSame(), 381–382
confirm(), 481–483
converting data types, 45–47
createPopup(), 485–487
Date, 248
defined, 96
Document object, 303
document.write() and document.writeln(), 368–369
DOM attribute, 375–376
drawImage(), 806–807
ECMAScript 5 extensions to array, 231–245
explicit type conversion, 105–106
find(), 510
Form object, 551
functions using call(), 176–177
functions vs., 100
getCurrentPosition(), 735, 736
getElementById(), 335
getElementsByName(), 344–345
getElementsByTagName(), 347–348
Global, 253–256
HTML-related string, 262–264
HTML5 form validation, 606
ImageData API, 808
insertAdjacentHTML() and insertAdjacentText(), 367–368
insertBefore(), 360
javaEnabled(), 724
JSON.stringify(), 678–680
Location object, 518
match(), 292–293
Math object, 257
Media object, 831
moveBy()/moveTo(), 511
object, 189
overriding default object, 210
parse() method for JSON, 680
postMessage(), 536–538
print(), 509
prompt(), 483–484
pushstate(), 522, 523–525, 548
querySelector() and querySelectorAll()
selector, 353–356
reading XHR response headers, 682
removeEventListener(), 444–445
replace(), 292
resizeBy() and resizeTo(), 512
resolveURL(), 519
scroll() and scrollBy(), 512–516, 517
setRequestHeader, 667–668
showModalDialog(), 484–485
showModeless Dialog(), 485
stop(), 509
Storage object, 759
submit(), 551
supporting DOM namespaces, 382–383
Text API methods for canvas element, 811
transformation, 806
using Date.parse(), 250–251
watchPosition(), 736–737
Window object, 478–480
XDR, 696
XMLHttpRequest object, 656
MIME types
defined, 833–834
detecting browser support for, 834–837
printing content of mimeTypes[ ] array, 834, 835
registering with content handlers, 748
setting for returned data, 686–687
minification of names, 109
mitered lines, 793
miterLimit property, 793
mobile technology
geolocation for, 734–737
media queries for, 733–734
modal dialogs, 484–485
modeless window dialog, 485
modulus (%) operators, 123, 124
monkey patching, 26
mouse events, 453–458
initiating, 455–456
list of, 453
supporting mouse wheel events, 456–458
moveBy()/moveTo() methods, 511
moving windows on screen, 511
multidimensional arrays, 230
multiline matching in regular expressions, 293–294
multiplication (*) operators, 123, 124
N
name attribute
accessing form with, 552–553
accessing frames by, 540
associative arrays with, 309
matching id attributes, 308
working with radio button, 569
namespaces
methods supporting DOM, 382–383
object wrappers for, 28–29
naming
camel-back, 39
events, 416
global variables, 110
minification and obfuscation of names, 109
properties, 187
NaN constant
defining with Global object, 253
native browser access in Ajax, 693, 694
native JSON support, 75
Navigation Timing API, 740–742
Navigator object
about, 301
browser detection results using, 716, 717
content handler methods for, 748
indicating browser type with, 716–719
initial, 300
javaEnabled() method for, 724, 850
Netscape’s influence in JavaScript’s, 300
properties of, 717
registerProtocolHandler method for, 746–747
userAgent property of, 717, 718
visually detecting, 730
negating values, 126
negative character classes, 278
Netscape
about Netscape browser family, 23–24
communicating with applets, 850
event model for, 432–434
evolution of DHTML object models within, 324
extended object model properties for, 320, 321
influence in JavaScript’s Navigator object, 300
JavaScript object model introduced in Netscape 2, 299
traditional object models in, 319–320
networks
challenges handling Ajax over, 713
checking online status for browsers, 738–739
determining connection with script timer, 739–740
effect of conditions on JavaScript elements, 614
network state properties for video, 826
new operator, 138
NodeIterator object, 410–411
nodes
appending and inserting, 358
characteristics of common HTML, 349, 350
copying, 370
creating, 356–357
defined, 332
deleting and replacing, 370–372
extending and inserting code after, 360–362
making attribute, 376–380
modifying text, 373–375
normalizing appended child, 358–360
types of DOM, 333–334
noncapturing parentheses, 294
nonstandard events, 464
normalizing appended child nodes, 358–360
<noscript> element
checking JavaScript support with, 721–722
providing for browsers not supporting JavaScript, 20–22
NOT (tilde) operator, 127, 128
novalidate attribute, 608
null data type
converting to other data types, 103–104
setting multiple references to null, 186
number data types, 84–89
bitwise operators for, 126–128
converting other types to, 103
defined, 43
floating-point decimals and, 84, 257
hexadecimal literals, 84–85
integer values of, 84
JSON, 677
number object, 87–88
octal literals, 85
representing integers accurately, 88–89
special values for, 85–87
using Number built-in object, 258
Number object, 258
O
obfuscation techniques, 109, 884–885
object models. See also DOM
about JavaScript, 297–299
DHTML, 322–323
event handling, 312–316
evolution of JavaScript, 319–325
extended Netscape 3, 320
illustrated, 298
initial, 299–301
Internet Explorer 4, 322–323, 324
traditional, 319–320
using components of, 316–319
object operators, 137–139
object-oriented JavaScript features, 197–210. See also object-related statements
about, 49–50
adding methods with object prototype, 201–203
constructor property for prototypes, 206–208
examining internal prototypes, 208
inheritance via prototypes, 205–206
new ECMAScript 5, 210–215
OOP principles supported, 215
overriding default methods and properties, 210
prototype-based objects, 198–199
treating functions as objects, 169–177
uses for, 197–198
using Object.create() pattern, 209–210
object-related statements, 154–157
about, 154
for-in statements, 156–157
indicating objects using with statements, 155–156
try...catch and throw, 157
Object superclass, 198–199
Object.create() pattern, 209–210
<object> tag, 849–850
objects. See also and specific objects; arrays; object-related statements
about JavaScript, 96, 181–183, 215–216
browser and document, 96–97
characteristics of JavaScript, 47–50
common properties and methods of, 196–197
comparing, 194–196
construction and conversion to native, 200–201
conversion to ISO format, 75
converting to other data types, 103–104
creating, 97–98, 183–184, 199–200
Date, 246–253
destruction and garbage collection of JavaScript, 185–186
detecting arrays, 100–101
DOM collections of, 345–346
ECMAScript built-in and host, 50–51
ECMAScript 5 array, 75
enumerating properties for, 189–190
finding existing properties for, 187–188
functions, 99–100
Global, 253–256
inheritance for, 205–206, 209–210
JavaScript’s object-oriented approach to, 197–210
JSON, 677–678
Math, 256–258
methods for, 189
multiple property definitions for, 211–212
new ECMAScript 5 features for, 76, 210–215
Number, 258
object literals, 184–185
overriding default methods and properties, 210
performance when overusing, 904
properties of, 186–187
prototype-based, 198–199
prototypes for, 201–203
reference types as, 191
references properties used with, 190–191
regular expression, 53
sealed and frozen, 213
short-circuit evaluation to detect, 145
String, 259–265
treating functions as, 169–177
trimming whitespace from String objects, 76
typeof operators for, 100
types of events for, 312
using RegExp, 280–289
using wrappers to avoid namespace collisions, 28–29
valid property names for, 187
Window object properties and, 476–478
octal literals, 85
offline cached resources, 763–765
on/offline events, 470
onbeforeunload event, 468–469
onchange event handlers, 602–603
onclick events
event bindings for, 417
handling, 312–313
one-time pop-up windows, 755
onhashchange events, 521–522
onkeypress event handler, 603–604
online status for browsers, 738–739
onmouseover event, 815–816
onprogress event handlers, 689–690
onreadystatechange event, 469
onsubmit event handler, 551
opening/closing generic windows, 487–497
building contents for open windows, 499–500
closing window with close() method, 489–490
detecting and controlling window chrome, 497–498
parameters for window.open(), 490–497
setting URL when opening, 487–488
syntax for opening window, 487–488
operators, 122–141. See also arithmetic operators;
assignment operators; bitwise operators; and specific operators
array, 188–189
assignment, 40–41, 55–56, 119, 123
bitwise shift, 128–129
conditional, 56
defined, 38
optimizing runtime performance with ===
operators, 908
precedence and associativity of, 59–60, 139–141
relational, 58–59
shorthand assignment with arithmetic or bitwise, 129–130
type-related, 56–57
unary, 126
void, 136
<optgroup> tag, 572–573
options
accessing select menu, 571–572
adding and deleting menu, 573–574
grouping with <optgroup> tag, 572–573
options[ ] collection
if HTMLSelectElement properties, 571–572
inserting Option objects in, 574
OR (||) operations, 143–144
OR (|) patterns, 279–280
OR (!) operator, 128
order
array sort, 228–229
coding execution, 38
LIFO and FIFO, 223
outerHTML property, 365–366
outerText property, 366–367
overlays, 505–508
overrideMimeType() method, 656, 686
overriding
default methods and properties, 210
default values in prototypes, 202–203
P
packaging scripts into files, 901
page-load performance, 897–902
authoring code for load speed, 900–902
bootstrapping, 899
caching for, 902
dynamic script loading, 898–899
golden rule of Web performance, 898
Gzip compression of files, 901
lazy-loading techniques, 899–900
loading scripts, 898–900
minification, 900–901
packaging scripts into files, 901
preloading techniques, 900
page modal, 481
parameters
creating robust handling for, 174–177
separating with commas, 135
<param> tag, 308
parentheses ( ) operator, 139
parseFloat() method, 254
parseInt() method, 255
passing
arguments to functions, 99–100
object reference types to functions, 192–194
parameters to functions, 160–161, 173–174
primitive data types to functions, 162–164
password properties, 561
Path API, 794–797
paths
direct path styles for older browsers, 346
resetting with beginPath() method, 794–796
setting to external script, 14–15
using transform matrix for, 804–806
patterns
commonly used character class, 278–279
creating, 272
escape codes, 272–273
grouping characters in, 275–276
looking for negative character class, 278
positional indicators within regular expressions, 272
searching for alternative, 279
setting pattern attribute, 605
using Object.create(), 209–210
validating with character classes, 276–278
performance, 895–908
authoring code for load speed, 900–902
caching for, 902
enhancing with browser’s developer tools, 897
load time for Java applets, 851
measuring with Date.getTime() function, 895–896
page-load, 897–902
runtime, 902–908
trade-offs when adding source security, 886
performance.navigation object, 742
period operator. See dot (.) operator
persistent cookies, 749
perspective in images, 800–802
phone number validation, 267–268, 276–277
pixel calculations for onmouseover event, 815–816
playbackRate property, 826
plug-ins, 832–845
about, 832
Active X controls vs., 845
controlling Flash, 840–845
dealing with Internet Explorer, 839
detecting support for MIME types, 833–837
embedding content for, 833
inspecting properties of Plugin object, 837–838
JavaScript’s interactions with, 839
refreshing plugins[ ] array, 839–840
pluginspage attribute, 839
plus (+) operator, 104
polyfills, 576
polymorphism, 215
pop-up blockers, 503–505
pop-up dialogs, 485–487
Position object, 735
PositionError object, 736
postMessage() method, 536–538
pre- and post-increment/decrement operators, 130–131
precedence of operators, 59–60, 139–141
preloading techniques, 900
primitive data types
composite data types vs., 96
converting, 102–104
number, 84–89
passing parameters with, 192–193
passing with functions, 162–164
promoting to objects, 104
string, 89–94
Print dialog, 509
print() method, 509
printCoords() function, 824
printCoords() method, 816–817
printing
content of mimeTypes[ ] array, 834, 835
web pages, 743
progress bars, 645–646
progressive enhancement
creating accordion widget using, 616–621
<progress> tag, 720–721
prompt() method, 483–484
properties. See also specific object properties
common object, 196–197
defined, 96
defining multiple, 211–212
defining prototype class properties, 204–205
detecting object’s existing, 187–188
document object’s lowest common denominator, 302
enumerating object, 189–190
frame, 539
getters and setters defined for, 212
instantiating browser’s object, 305–306
Netscape extended object model, 320, 321
object, 186–187
overriding default object, 210
removing from objects, 87
valid names for, 187
Window object, 476–478
property descriptors, 211
property/value pairs, 184–185
_proto_ property, 208
protocol handlers, 746–747
prototype property, 201
prototype-based objects, 198–199
prototypes
adding property to all object instances with, 186
constructor property for, 206–208
creating inheritance for with Object.
create() pattern, 209–210
defining class properties of, 204–205
examining internal, 208
extending arrays with, 224, 230–231
extending native object, 203–204
inheritance via, 205–206
instanceof operator with, 207–208
overriding default values in, 202–203
prototype-based objects, 198–199
using object, 201–203
proxy approach in Ajax, 693, 694
pseudo-URLs, 17–19
pull-down menus, 570–572
push-style communications
binary bridge, 693, 694, 703, 704
fast and long polls in, 703, 704, 705–707
types of approaches in, 704
pushstate() method, 522, 523–525, 548
Q
quadraticCurveTo() method, 793, 794
querySelector() method, 353–356
querySelectorAll() method, 353–356
queues, 223–224
quotation marks, 91–92
R
radial gradients, 798, 799, 800
radio buttons
attributes for, 568
collections of, 569–570
HTML syntax for, 567
random number generation, 258
read-only form fields, 587, 588
reading/writing existing windows, 500–501
readyState values
browser modification of, 652
defined, 655
testing, 668–670
recursion and runtime performance, 904
recursive functions, 177–179
redirecting users to web pages, 754–755
redrawing images on canvas, 818–820
reduce() method, 241–243
reduceRight() method, 243–245
reducing lookups, 905
reference types, 191–196
refreshing
plugins[ ] array, 839–840
web pages, 518
declaring regular expressions with, 269
syntax for, 269
RegExp objects, 280–289
compile() method for, 282
exec() method for, 282–283
global flag for exec() method, 283–286
properties of, 286–289
properties of RegExp () constructor, 287, 288
search() method for, 290
split() method for, 290–291
string methods for, 290–293
subexpressions for, 281
test() method for, 280–281
registerProtocolHandler() method, 746–747
regular expressions, 267–296. See also RegExp objects
about, 267
character classes for, 276–279
comparing, 270–271
creating, 71
creating patterns with, 272–280
escape codes in, 272–273
examples of, 280
flags altering interpretation of, 269
form validation using, 591–594
greedy matching, 295
grouping characters in, 275–276
limitations of, 296
lookahead expressions, 294–295
match() method in, 292–293
multiline matching in, 293–294
negative character classes, 278
noncapturing parentheses in, 294
positional indicators for, 272
regular expression literals, 53
repetition quantifiers in, 274–275, 296
running typeof operators on, 100
searching for alternative patterns, 279–280, 296
string methods for, 290–293
syntax for, 268–269
uses for, 267–268
using RegExp objects, 280–289
relational operators, 58–59
relationships between frames, 538–543
remove() method, 574
removeEventListener() method, 444–445
removing array elements, 187, 220
repetition quantifiers, 274–275, 296
replace() method
for String objects, 291
with subexpressions, 292
replacestate() method, 522, 523–525
replacing nodes, 370–372
requestAnimationFrame() technique, 821–822, 824
requests. See also XMLHttpRequest object
asynchronous XHR, 662–663
cross-domain, 695–698
Cross-Site Request Forgery, 893–895
managing XHR objects, 713
POST, 665–666
sending data via GET, 664–665
synchronous XHR, 661–662
required attribute, 604
reserved literals, 914
reserved words
about JavaScript, 913
browser inconsistencies handing, 915–917
defined, 38
resizing windows, 511–512
resolveURL() method, 519
response headers for XHRs, 682
response property, 655, 674–675
responseText property, 655, 671
responseType property, 655, 674–675
responseXML property, 655, 671–674
return statements
about, 68
saving results of function with, 161–162
reverse() method, 226–227
rollovers, 775–780
routing, simulating event, 441
runtime performance, 902–908
avoiding excessive function calls, 903–904
coding to improve, 902–903
effect of object overuse on, 904
event delegation for, 906–907
excessive loops diminishing, 904
limiting recursion for improved, 904
memory management for optimal, 907
minimizing bottlenecks accessing DOM, 905–906
small optimizations for, 908
strategies optimizing, 905
S
same-origin policy, 886–888
sandboxed natives, 204
sandboxing and iframes, 890–891
saving
browsers state values to user’s disk, 525
Canvas API’s state, 814
canvas changes, 820
scaling drawings, 802–803, 806
scope
event handler, 428–429
Screen object, 725–726
script element
adding JavaScript with, 8–12
differentiating JavaScript and XHTML with, 3–4
dynamically creating, 899
indicating language with type attribute, 9–10
placing in <head> tag, 11–12
setting language attribute for, 9
throwing errors for markup text, 4–8
type attribute used in, 9
script masking, 19–20
script timers, 739–740
scripts. See also script element; <script> tag
browser execution order of, 765–766
cross-site, 891–893
defensive programming for, 873–874
deferring execution order for browser, 765–766
dynamic loading of, 898–899
employing iframes for security, 890–891
JavaScript’s security policy for, 886
lazy-loading techniques, 899–900
linked, 16–17
loading, 898–900
packaging into files, 901
preloading, 900
scripting select menus, 573–574
using only trusted external, 888–890
<script> tag
addressing XSS by removing from posts, 892–893
Ajax workaround approach for, 693, 694
communications with, 701–702
detecting JavaScript versions with language attribute, 722–723
placing defer attribute on, 898
scrollbars, 497–498
scrolled lists, 570
scrolling windows, 512–516
sealed and frozen objects, 213
search bars, 744–746
search fields, 564
search() method for String objects, 290
security. See also cookies
about JavaScript, 883–884
avoiding leakage of history information, 304
concerns for windows, 498
Cross-Site Request Forgery, 893–895
cross-site scripts, 891–893
encoding and encrypting JavaScript, 885–886
exceptions to same-origin policy, 888
issues for cookies, 754
malicious bookmarklets, 17
obfuscation techniques for, 884–885
performance trade-offs with source, 886
risks using responseText, 671
same-origin policy for JavaScript, 886–888
sandboxing and iframes, 890–891
triggering ActiveX security dialog, 658
trusted external scripts, 888–890
select menus
accessing options on, 571–572
properties of Option object, 572–573
pull-down menus, 570–572
scripting, 573–574
semantic errors, 866–869
semantic text fields, 563–565
semicolons (;)
separating statements with, 41–42, 120–121, 157–158
terminating statements with, 120–121
sendRequest() function, 650, 651
server push, 687
Ajax-style applications and, 648
JavaScript’s future in, 908–909
validation during, 609
session cookies, 749
setInterval() method
implementing animation with, 784, 785
requestAnimationFrame() alternative to, 821–822
setRequestHeader method, 667–668
setTimeout() method
implementing animation with, 784, 785
SVG’s use of, 824
shapes
drawing, 792–793
example code for drawing, 798–799
making rectangles using canvas element, 789–791
SVG for drawing, 823
short-circuit evaluation of logical expressions, 143–145
showModalDialog() method, 484–485
showModeless Dialog(), 485
sibling nodes, 334
simulating browser button presses, 743–744
sliders in HTML5, 577–578
slow load
using, 707–709
smiley face, 794–796
sockets
Flash-based, 712
WebSocket communications, 709–712
some() method, 237–238
sort() method, 228–229
special characters
grouping, 275–276
JavaScript support for character sets, 90–91
positional indicators, 272
regular expression escape codes, 272–273
repetition quantifiers, 274–275
spin boxes, 564
splice() method, 227–228
split() method for String objects, 290–291
splitting strings, 261–262
square line cap property, 792
src attribute
setting media resource address with, 825
setting path to external script with, 14–15
using with linked scripts, 13–15
stacks
treating arrays as, 223–224
using stack trace, 870–871
state. See also cookies
browser state and loading events, 468–470
customizing web pages with cookies, 755–757
DOM Storage, 758–760
managing browser’s, 749
managing in frames, 546–548
redirections using cookies, 754–755
required for rollover buttons, 775
saving for canvas element, 814
saving site resources offline with application cache, 763–765
setting video network state properties, 826–827
using one-time pop-up windows, 755
statements
basic, 119
continue and break, 63–64, 152–154
do-while loops, 150–151
flow control for continue and break, 153–154
grouping lists in curly braces, 42, 121–122
if, 141–143
including return statements in functions, 161–162
object-related, 154–157
separating with semicolons, 41–42, 120–121, 157–158
short-circuit evaluation of if, 143–145
switch, 145–148
terminating, 120–121
while loops, 148–150
whitespace and syntax errors, 120
writing loop, 61–62
static variables, 172–173
status messages, 585–587
statusText property, 655, 670–671
stop() method, 509
Storage object, 758–760
strict equality (===) operator, 58, 94, 95, 132
strict inequality (!===) operator, 58, 132
strict mode
invoking for functions, 175
using with variables, 111–112
String built-in object, 259–265
string data type. See also strings
converting to other data types, 103–104
defined, 43
String objects
about, 259
match() method, 292–293
replace() method for, 291–292
search() method for, 290
split() method for, 290–291
strings associated with, 89–90
String split() method, 261
stringify(), method, 678–680
strings, 89–94
about String object, 259
associated with String object, 89–90
changing and joining array to, 226
comparing, 133
converting dates to, 249–250
defined, 89
ECMAScript 5 changes to, 264–265
escape codes for quotes in, 91–92
examining, 259–261
HTML whitespace handling in, 92–94
JSON, 676–677
lexicographic comparisons of, 58–59, 133
marking up as HTML, 262–264
methods for regular expressions, 290–293
search() method for, 290
split() method for, 290–291
splitting, 261–262
turning into dates, 250–251
type conversion to, 104
whitespace in, 40
<strong> tags, 4–8
style property, 397–403
subexpressions
about RegExp, 281
replace() method, 292
submenus, 630–631
Submit button, 557, 558, 608–609
submit() method, 551
subtraction (-) operator, 123, 124, 126
subtrees, 332
SVG
animating events with, 823–824
defined, 822
drawing with, 823
including document in HTML page, 822–823
SWF files, 842–843
switch statements, 145–148
synchronous XHR requests, 661–662
syntax errors, 865
T
tables in DOM, 394–396
tags. See also specific tags
associating event handlers with, 12
creating window within event handler attribute of, 490
inserting in front of other tags, 360
JSDoc comment, 859–860
targets, 416
terminating statements with semicolons, 120–121
test() method for RegExp objects, 280–281
testing
Ajax readyState values, 668–670
animation layers with DHTML, 784–788
browsers for plug-in support, 725
Canvas API hit, 818
Flash support, 725
importance of performance, 903
text fields, 562–563
text. See also text areas; text fields
adding to drawing in Canvas API, 808–812
alternatives for button, 559
click-to-edit scenarios for, 639–643
customizing font, 809
editing interface, 641–643
types of text events, 462–464
Text API, 811
text areas
defined, 560
<textarea> tags, 565–567
blur() and focus() methods for, 562
HTML5 semantic, 563–565
testing, 562–563
traditional, 560–563
text nodes, 335–336
<textarea> tags, 565–567
3D images
future implementation of, 788
using perspective with canvas element, 800–802
throwing
Error instance, 878
errors for markup text, 4–8
exceptions, 876
time zones, 246
timeouts
coding timers, 528–530
creating getCurrentPosition(), 735
intervals in timed events, 528
syntax for triggering, 527–528
timing Web page performance, 739–742, 896
“tip of the day” pages, 755
title attribute, 606
toDataUrl() method, 820
tokens
defined, 37
tool tips for fields, 585
toolbars, 497–498
tools for browser development, 897
toSource() method, 228
toString() method, 228
traditional event model, 417–432. See also events binding event handlers with JavaScript, 425–428
effect of return values on default behavior, 429–430
event binding in HTML5, 417
firing events manually, 430–432
HTML4 core events, 418–421
HTML5 core events, 421–424
overview of, 433
shortcomings of, 432
traditional object models, 319–320
transformations
customizing drawings with, 802–806
methods for, 806
translating images, 803–804, 806
transparency in linear gradients, 797–798
tree walking. See also document trees
common starting points for, 348–349
document trees and, 336–341
TreeWalker object, 410–411
triggering
events, 416
request in “Hello World” example, 650
timeouts, 527–528
trusted external scripts, 888–890
truth tables for bitwise operators, 127
try/catch blocks
catching same-origin policy errors with, 887
creating XMLHttpRequest (XHR) object with, 650
syntax for exceptions using, 877–878
type attribute, 9–10
type conversion
about, 118
explicit, 105–106
promoting primitive data to objects, 104
rules for primitive types, 102–104
unary operators and, 126
type-related operators, 56–57
typeof operators
for objects, 100
return values for, 136–137
U
UFO animation, 784–788
UI events, 458–459
unary operators, 126
undefined data type
converting to other data types, 103–104
unregisterContentHandler() method, 748
untyped languages, 82
update meters, 645–646
upload property, 655
uploading files
drag-and-drop, 636–638
with <input> tag, 578–579
using <iframe>, 691–692
Ajax’s URL forwarding approach, 693, 694
displaying returned XML packet in browser, 650–651
hash values in, 520–522
linking protocol handlers to specific, 746–747
registering mime type handled by specific, 748
revealing past browsing habits in JavaScript, 51
same-origin checks for, 887
setting modal dialog, 484–485
setting when opening generic windows, 487–488
user-agent strings
browser detection with, 716
userAgent property of Navigator object, 717, 718
user-defined objects, 181, 182
user interface, 611–646
adding JavaScript in, 611–616
Ajax improvements to, 713
content editing, 639–643
graceful degradation in, 612, 616, 621–623
handling drag-and-drop functionality for, 631–639
network conditions and, 614
progressive enhancement of, 612, 616–621
revealing content on demand, 644
update meters and progress bars, 645–646
updates in Ajax-style applications, 647–649
working with HTML5 menus and context menus, 623–631
users
detecting preferred browser language for, 738
providing different content for offline, 738–739
redirecting to more appropriate web pages, 754–755
viewing geolocation of, 734–737
“use strict”; mode, 74, 110. See also strict mode
UTF-8 character encoding, 671
V
validation. See also form validation
client-side data, 31, 597, 610
improved HTML5, 604–606
phone number, 267–268, 276–277
regular expression patterns for, 272, 276–278
using character classes, 276–278
zip code, 602–603
ValidityState object, 606, 607
value attribute, 568
value property of Select object, 571
values
negating, 126
reducing results of array, 241–243
selecting multiple field, 579
var statements, 70–71
variables
about, 106
capitalizing, 108–109
dash separators in, 109
defined, 81
event handlers and variable scope, 114–116
execution contexts for, 116–117
identifiers for, 106
implicit declaration of, 111
invoking functions, 112–114
JavaScript use with other scripting, 28–29
limiting scope of, 167–168
minification and obfuscation of names, 109
passing to functions, 160–161
static, 172–173
strict mode with, 111–112
underlying attributes of JavaScript, 211
using, 42–43
VBScript
indicating use of, 9
support for ActiveX, 849
vector graphics with SVG, 822–824
Vector Markup Language (VML), 788
versions. See also specific HTML versions
ECMAScript, 22–23
Internet Explorer JScript/JavaScript compatibility, 24–26
Netscape/Mozilla/Firefox JavaScript, 23–24
<video> tag, 825–830
code example for, 827–828
media object properties applicable to, 829–830
methods of Media object, 831
network state properties for playing, 826–827
properties of video object, 828
setting playback and volume of, 826
setting poster for, 826
visible property
controlling menu visibility with, 498
revealing content on demand with, 644
visually detecting browsers, 725–730
VML (Vector Markup Language), 788
W
W3C (World Wide Web Consortium)
DOM Element Traversal Specification, 342–343
DOM level definitions, 327–328
Navigation Timing API, 740–742
requiring type attribute in script element, 9–10
watchPosition() method, 736–737
Web applications Ajax-style communication flows for, 33
HTML5 and, 34
traditional communication flow in, 32–33
Web pages. See also page-load performance
accessing and setting location of, 516–520
adding JavaScript in user interface of, 611–616
Ajax-style communication flows and, 648
creating search pages, 744–746
customizing with cookies, 755–757
embedding ActiveX control in, 845–846
ensuring cross-browser compatibility of, 846–847
integrating objects of various namespaces within, 382–383
loading by clicking button, 517–518
model for Ajax-style applications, 647–649
one-time pop-up windows, 755
providing offline users different content, 738–739
redirecting users to, 754–755
refreshing, 518
sharing workers, 769–770
timing loading of, 739–742, 896
traditional communication flow in, 647, 648
video for, 825–830
Web Performance Working Group, 896
Web sites. See also Web pages
adding automatic error reporting to, 874–875
golden rule of Web performance, 898
saving resources offline with application cache, 763–765
Web workers
asynchronous and concurrent processing with, 767–768
sharing, 769–770
WebSocket communications, 703, 704, 709–712
while loops, 148–150
whitespace
handling in XHTML strings, 92–94
syntax errors and, 120
whitespace characters, 40
width
detecting and resizing browser window size, 726–730
image button, 559
Window object
about, 475–476
dialogs, 480–487
methods of, 478–480
onerror handler of, 874
properties and objects of, 476–478
proprietary window events added to, 534
Screen object child of, 725–726
setting timeouts and intervals for, 527–530
window.performance object, 740–742, 896
windows. See also dialogs; frames; opening/closing generic windows; window object accessing and setting location of, 516–520
building contents of, 499–500
centering, 502–503
closing, 489–490
communicating among, 534–536
controlling browser status bar, 525–527
dealing with pop-up blockers, 503–505
detecting and controlling chrome, 497–498
dialogs, 480–487
find() method for, 510
focus() and blur() methods for, 509
frame building and frame busting, 545–546
full-screen, 501
hash values in URLs, 520–522
HTML5 and proprietary window events, 530–534
manipulating history for, 522–527
moving on screen, 511
opening/closing generic, 487–497
passing data between, 536–538
print() method for, 509
reading/writing existing, 500–501
resizing, 511–512
scrolling, 512–516
security concerns for, 498
setting parameters for open, 490–497
setting timeouts and intervals, 527–530
stop() method, 509
triggering creation of, 488
using overlays vs., 505–508
window.sizeToContent() method, 493
with statements
indicating objects using, 155–156
referencing object properties using, 190–191
Worker constructor, 767–768
World Wide Web Consortium. See W3C
wrappers
avoiding namespace collisions with, 28–29
Internet Explorer event handler, 314–315
using with ActiveX controls, 659–660
writing JavaScript. See also expressions; operators; statements
about, 855
about Cross-Site Request Forgery, 893–895
accepting JavaScript’s language flaws, 855, 911
authoring for load speed, 900–902
case sensitivity of JavaScript, 38–39
catching exceptions with try/catch syntax, 877–878
coding style, 856–858
comments and documentation, 73, 858–861, 862
common errors, 867–868
common JavaScript terms, 37–38
debugging output manually, 869–870
defensive programming, 873–878
efforts to fix JavaScript, 909–911
embedding JavaScript in HTML, 39
examples of error messages, 864
execution order in code, 38
flow control for loops, 63–64
functions, 68–70
future trends, 908–911
HTTP-only cookies, 893
input and output, 64–68
JavaScript expressions, 53
loops, 61–62
obfuscation techniques, 884–885
objects, 47–50
operators, statements, and expressions in, 119, 157–158
optimizing runtime performance, 902–908
performance considerations for, 895–908
primitive data types, 43–44
regular expressions, 71
runtime errors, 865–866
scoping rules for JavaScript, 70–71
security when, 883–886
selecting libraries when, 879–893
semantic errors, 866–869
separating statements with semicolons, 41–42, 120–121, 157–158
statements, 40–41
syntax errors when, 865
turning on error messages, 861–863
type conversion in JavaScript, 44–47, 101–106
using debuggers, 871–872
using flow control in, 60–61
variables, 42–43
weak typing in JavaScript, 44, 82–83
whitespace characters in, 40
XDomainRequest (XDR) object, 695–698
XHR object. See XMLHttpRequest object
XHTML
adding JavaScript to documents, 8–19
errors using JavaScript with markup for, 4–8
whitespace handling in strings, 92–94
XMLHttpRequest (XHR) object, 654–657
asynchronous requests for, 662–663
authentication with, 684–686
browser support for, 654
communications not using, 698–702
controlling requests for, 683–684
cross-domain requests vs. using, 692–695
HTTP methods supported by, 666
instantiation of, 657–658
managing MIME types, 686–687
methods of, 656
onload, onloadstart, and onloadend events for, 687–688, 696
onprogress event handler support by browsers, 689–690
performing form serialization, 690–692
properties of, 655
response headers for, 682
setRequestHeader() method for, 667–668
susceptibility to hijacking, 890
synchronous requests for, 661–662
try-catch blocks to create, 650
using with ActiveX in Internet Explorer, 658–659
UTF-8 character encoding when implementing, 671
working with multipart responses, 687
wrapper functions for cross-browser support, 659–660
XOR (^) operator, 128
XSS (cross-site scripts), 891–893