├── .gitignore ├── es-262 ├── ECMA-262 5.1 edition June 2011.pdf ├── ECMA-262 5th edition December 2009.pdf ├── ECMA-262, 1st edition, June 1997.pdf ├── ECMA-262, 2nd edition, August 1998.pdf └── ECMA-262, 3rd edition, December 1999.pdf ├── js-1.0 ├── alpha.html ├── builtin.html ├── colors.html ├── content.html ├── cookie_spec │ ├── assist_ban.gif │ └── cookie_spec.html ├── events.html ├── expr.html ├── ident.html ├── index.html ├── introd.html ├── keywords.html ├── methods.html ├── model.html ├── navbar.html ├── navobj.html ├── objects.html ├── props.html ├── ref_a-c.html ├── ref_d-e.html ├── ref_f-g.html ├── ref_h-l.html ├── ref_m-q.html ├── ref_r-r.html ├── ref_s-s.html ├── ref_t-z.html ├── script.html ├── stmts.html ├── stmtsov.html ├── toc.html └── winframe.html ├── js-2.0 └── javascript2.0_ evolving_a_language_for_evolving_systems.pdf └── misc └── ES4-language-overview.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /es-262/ECMA-262 5.1 edition June 2011.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rwaldron/js-history/b62e5b6c8c80c2d95b47518456f3fb00470122d2/es-262/ECMA-262 5.1 edition June 2011.pdf -------------------------------------------------------------------------------- /es-262/ECMA-262 5th edition December 2009.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rwaldron/js-history/b62e5b6c8c80c2d95b47518456f3fb00470122d2/es-262/ECMA-262 5th edition December 2009.pdf -------------------------------------------------------------------------------- /es-262/ECMA-262, 1st edition, June 1997.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rwaldron/js-history/b62e5b6c8c80c2d95b47518456f3fb00470122d2/es-262/ECMA-262, 1st edition, June 1997.pdf -------------------------------------------------------------------------------- /es-262/ECMA-262, 2nd edition, August 1998.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rwaldron/js-history/b62e5b6c8c80c2d95b47518456f3fb00470122d2/es-262/ECMA-262, 2nd edition, August 1998.pdf -------------------------------------------------------------------------------- /es-262/ECMA-262, 3rd edition, December 1999.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rwaldron/js-history/b62e5b6c8c80c2d95b47518456f3fb00470122d2/es-262/ECMA-262, 3rd edition, December 1999.pdf -------------------------------------------------------------------------------- /js-1.0/alpha.html: -------------------------------------------------------------------------------- 1 |
2 |6 | The JavaScript Language contains the following built-in objects and functions: 7 |
14 | These objects and their properties and methods are built into the language.
15 | You can use these objects in both client applications with Netscape Navigator
16 | and server applications with LiveWire.
17 |
18 | Using the String Object
19 |
20 | Whenever you assign a string value to a variable or property, you create a string object. 21 | String literals are also string objects. For example, the statement 22 |
23 | mystring = "Hello, World!" 24 |25 |
26 | creates a string object called mystring. The literal "blah" is also a string object. 27 | 28 |
29 | The string object has methods that return: 30 |
35 | For example, given the above object, mystring.toUpperCase()
returns "HELLO, WORLD!",
36 | and so does "hello, world!".toUpperCase()
.
37 |
38 |
39 | Using the Math Object
40 |
41 | The built-in Math object has properties and methods for mathematical constants and 42 | functions. For example, the Math object's PI property has the value of pi, which you 43 | would use in an application as 44 |
45 | Math.PI 46 |47 |
48 | Similarly, standard mathematical functions are methods of Math. 49 | These include trigonometric, logarithmic, exponential, and other functions. 50 | For example, if you want to use the trigonometric function sine, you would write 51 |
52 | Math.sin(1.56) 53 |54 |
55 | Note that all trigonometric methods of math take arguments in radians. 56 |
57 | It is often convenient to use the with statement when a section of code uses several math 58 | constants and methods, so you don't have to type "Math" repeatedly. For example, 59 |
60 | with (Math) { 61 | a = PI * r*r; 62 | y = r*sin(theta) 63 | x = r*cos(theta) 64 | } 65 |66 | 67 |
69 | JavaScript does not have a date data type. However, the date object and its 70 | methods enable you to work with dates and times in your applications. 71 | The date object has a large number of methods for setting, getting, 72 | and manipulating dates. It does not have any properties. 73 |
74 | JavaScript handles dates very similarly to Java. 75 | The two languages have many of the same date methods, and both languages 76 | store dates as the number of milliseconds since January 1, 1970 00:00:00. 77 |
78 | NOTE: 79 | You cannot currently work with dates prior to 1/1/70. 80 | 81 |
To create a date object: 82 |
83 | varName = new Date(parameters) 84 |85 | where varName is a JavaScript variable name for the date object being created; 86 | it can be a new object or a property of an existing object. 87 |
88 | The parameters for the Date constructor can be any of the following: 89 |
today = new Date()
92 |
95 | Xmas95= new Date("December 25, 1995 13:30:00")
96 |
97 | If you omit hours, minutes, or seconds, the value will be set to zero.
98 |
99 |
102 | Xmas95 = new Date(95,11,25)
103 |
104 |
107 | Xmas95 = new Date(95,11,25,9,30,0)
108 |
109 | 111 | The Date object has a large number of methods for handling dates and times. 112 | The methods fall into these broad categories: 113 |
120 | The "get" and "set" methods enable you to get and set seconds, minutes, hours, 121 | day of the month, day of the week, months, and years separately. 122 | There is a getDay method that returns the day of the week, but no 123 | corresponding setDay method, because the day of the week is set automatically. 124 | These methods use integers to represent these values as follows: 125 |
134 | For example, suppose you define the following date: 135 |
136 | Xmas95 = new Date("December 25, 1995") 137 |138 | Then
Xmas95.getMonth()
returns 11, and Xmas95.getYear()
139 | returns 95.
140 | 141 | The getTime and setTime methods are useful for comparing dates. 142 | The getTime method returns the number of milliseconds since the epoch for 143 | a date object. 144 |
145 | For example, the following code displays the number of shopping days left until 146 | Christmas: 147 |
148 | today = new Date() 149 | nextXmas = new Date("December 25, 1990") 150 | nextXmas.setYear(today.getYear()) 151 | msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day 152 | daysLeft = (nextXmas.getTime() - today.getTime()) / msPerDay; 153 | daysLeft = Math.round(daysLeft); 154 | document.write("Number of Shopping Days until Christmas: " + daysLeft); 155 |156 |
157 | This example creates a date object named today that contains today's date. 158 | It then creates a date object named nextXmas, and sets the year to the 159 | current year. Then, using the number of milliseconds per day, it computes 160 | the number of days between today and nextXmas, using getTime, and rounding 161 | to a whole number of days. 162 |
163 | The parse method is useful for assigning values from date strings to existing 164 | date objects. For example, the following code uses parse and setTime to 165 | assign a date to the IPOdate object. 166 |
167 | IPOdate = new Date() 168 | IPOdate.setTime(Date.parse("Aug 9, 1995")) 169 |170 | 171 | 180 | 181 |
184 | JavaScript has several "top-level" functions built-in to the language. 185 | They are: 186 |
194 | The built-in function eval takes a string as its argument. 195 | The string can be is any string representing a JavaScript expression, statement, 196 | or sequence of statements. 197 | The expression can include variables and properties of existing objects. 198 |
199 | If the argument represents an expression, eval evaluates the expression. 200 | If the argument represents one or more JavaScript statements, eval performs the statements. 201 | 202 |
203 | This function is useful for evaluating a string representing an arithmetic expression. 204 | For example, input from a form element is always a string, but you often want to convert 205 | it to a numerical value. 206 |
207 | The following example takes input in a text field, applies the eval function and displays 208 | the result in another text field. 209 | If you type a numerical expression in the first field, and click on the button, the 210 | expression will be evaluted. For example, enter "(666 * 777) / 3", and click on the button 211 | to see the result. 212 | 213 |
228 | 229 | 234 |
241 | 242 |243 | The eval function is not limited to evaluating numerical expressions, however. 244 | Its argument can include object references or even JavaScript statements. 245 | For example, you could define a function called setValue that would take two arguments: 246 | and object and a value, as follows: 247 | 248 |
249 | function setValue (myobj, myvalue) { 250 | eval ("document.forms[0]." + myobj + ".value") = myvalue; 251 | } 252 |253 | 254 |
255 | Then, for example, you could call this function to set the value of a form element 256 | "text1" as follows: 257 |
258 | setValue(text1, 42) 259 |260 | 261 | 262 |
265 | These two built-in functions return a numeric value when given a string as an argument. 266 |
267 | ParseFloat parses its argument, a string, and attempts to return a floating point number. If it encounters 268 | a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns 269 | the value up to that point and ignores that character and all succeeding characters. If the first character 270 | cannot be converted to a number, it returns NaN (not a number). 271 |
272 | The parseInt function parses its first argument, a string, and attempts to return an integer of the 273 | specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 274 | 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater 275 | than 9. For example, for hexadecimal numbers (base 16), A through F are used. 276 |
277 | If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all 278 | succeeding characters and returns the integer value parsed up to that point. If the first character cannot 279 | be converted to a number in the specified radix, it returns NaN. ParseInt truncates numbers to integer 280 | values. 281 | 282 | 283 | -------------------------------------------------------------------------------- /js-1.0/colors.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 | Color values
6 |
7 |
The string literals in this table can be used to specify colors in the JavaScript alinkColor, bgColor, fgColor, linkColor, and vlinkColor properties and the fontcolor method. 8 | 9 |
You can also use these string literals to set the color in the HTML reflections of these properties, for example <BODY BGCOLOR="bisque">, and to set the COLOR attribute of the FONT tag, for example, <FONT COLOR="blue">color</font>. 10 | 11 |
The following red, green, and blue values are in hexadecimal. 12 | 13 |
14 |
17 | Color
18 | aliceblue 19 | antiquewhite 20 | aqua 21 | aquamarine 22 | azure 23 | beige 24 | bisque 25 | black 26 | blanchedalmond 27 | blue 28 | blueviolet 29 | brown 30 | burlywood 31 | cadetblue 32 | chartreuse 33 | chocolate 34 | coral 35 | cornflowerblue 36 | cornsilk 37 | crimson 38 | cyan 39 | darkblue 40 | darkcyan 41 | darkgoldenrod 42 | darkgray 43 | darkgreen 44 | darkkhaki 45 | darkmagenta 46 | darkolivegreen 47 | darkorange 48 | darkorchid 49 | darkred 50 | darksalmon 51 | darkseagreen 52 | darkslateblue 53 | darkslategray 54 | darkturquoise 55 | darkviolet 56 | deeppink 57 | deepskyblue 58 | dimgray 59 | dodgerblue 60 | firebrick 61 | floralwhite 62 | forestgreen 63 | fuchsia 64 | gainsboro 65 | ghostwhite 66 | gold 67 | goldenrod 68 | gray 69 | green 70 | greenyellow 71 | honeydew 72 | hotpink 73 | indianred 74 | indigo 75 | ivory 76 | khaki 77 | lavender 78 | lavenderblush 79 | lawngreen 80 | lemonchiffon 81 | lightblue 82 | lightcoral 83 | lightcyan 84 | lightgoldenrodyellow 85 | lightgreen 86 | lightgrey 87 | lightpink 88 | lightsalmon 89 | lightseagreen 90 | lightskyblue 91 | lightslategray 92 | lightsteelblue 93 | lightyellow 94 | lime 95 | limegreen 96 | linen 97 | magenta 98 | maroon 99 | mediumaquamarine 100 | mediumblue 101 | mediumorchid 102 | mediumpurple 103 | mediumseagreen 104 | mediumslateblue 105 | mediumspringgreen 106 | mediumturquoise 107 | mediumvioletred 108 | midnightblue 109 | mintcream 110 | mistyrose 111 | moccasin 112 | navajowhite 113 | navy 114 | oldlace 115 | olive 116 | olivedrab 117 | orange 118 | orangered 119 | orchid 120 | palegoldenrod 121 | palegreen 122 | paleturquoise 123 | palevioletred 124 | papayawhip 125 | peachpuff 126 | peru 127 | pink 128 | plum 129 | powderblue 130 | purple 131 | red 132 | rosybrown 133 | royalblue 134 | saddlebrown 135 | salmon 136 | sandybrown 137 | seagreen 138 | seashell 139 | sienna 140 | silver 141 | skyblue 142 | slateblue 143 | slategray 144 | snow 145 | springgreen 146 | steelblue 147 | tan 148 | teal 149 | thistle 150 | tomato 151 | turquoise 152 | violet 153 | wheat 154 | white 155 | whitesmoke 156 | yellow 157 | yellowgreen 158 | |
159 |
160 | Red
161 | F0 162 | FA 163 | 00 164 | 7F 165 | F0 166 | F5 167 | FF 168 | 00 169 | FF 170 | 00 171 | 8A 172 | A5 173 | DE 174 | 5F 175 | 7F 176 | D2 177 | FF 178 | 64 179 | FF 180 | DC 181 | 00 182 | 00 183 | 00 184 | B8 185 | A9 186 | 00 187 | BD 188 | 8B 189 | 55 190 | FF 191 | 99 192 | 8B 193 | E9 194 | 8F 195 | 48 196 | 2F 197 | 00 198 | 94 199 | FF 200 | 00 201 | 69 202 | 1E 203 | B2 204 | FF 205 | 22 206 | FF 207 | DC 208 | F8 209 | FF 210 | DA 211 | 80 212 | 00 213 | AD 214 | F0 215 | FF 216 | CD 217 | 4B 218 | FF 219 | F0 220 | E6 221 | FF 222 | 7C 223 | FF 224 | AD 225 | F0 226 | E0 227 | FA 228 | 90 229 | D3 230 | FF 231 | FF 232 | 20 233 | 87 234 | 77 235 | B0 236 | FF 237 | 00 238 | 32 239 | FA 240 | FF 241 | 80 242 | 66 243 | 00 244 | BA 245 | 93 246 | 3C 247 | 7B 248 | 00 249 | 48 250 | C7 251 | 19 252 | F5 253 | FF 254 | FF 255 | FF 256 | 00 257 | FD 258 | 80 259 | 6B 260 | FF 261 | FF 262 | DA 263 | EE 264 | 98 265 | AF 266 | DB 267 | FF 268 | FF 269 | CD 270 | FF 271 | DD 272 | B0 273 | 80 274 | FF 275 | BC 276 | 41 277 | 8B 278 | FA 279 | F4 280 | 2E 281 | FF 282 | A0 283 | C0 284 | 87 285 | 6A 286 | 70 287 | FF 288 | 00 289 | 46 290 | D2 291 | 00 292 | D8 293 | FF 294 | 40 295 | EE 296 | F5 297 | FF 298 | F5 299 | FF 300 | 9A 301 | |
302 |
303 | Green
304 | F8 305 | EB 306 | FF 307 | FF 308 | FF 309 | F5 310 | E4 311 | 00 312 | EB 313 | 00 314 | 2B 315 | 2A 316 | B8 317 | 9E 318 | FF 319 | 69 320 | 7F 321 | 95 322 | F8 323 | 14 324 | FF 325 | 00 326 | 8B 327 | 86 328 | A9 329 | 64 330 | B7 331 | 00 332 | 6B 333 | 8C 334 | 32 335 | 00 336 | 96 337 | BC 338 | 3D 339 | 4F 340 | CE 341 | 00 342 | 14 343 | BF 344 | 69 345 | 90 346 | 22 347 | FA 348 | 8B 349 | 00 350 | DC 351 | F8 352 | D7 353 | A5 354 | 80 355 | 80 356 | FF 357 | FF 358 | 69 359 | 5C 360 | 00 361 | FF 362 | E6 363 | E6 364 | F0 365 | FC 366 | FA 367 | D8 368 | 80 369 | FF 370 | FA 371 | EE 372 | D3 373 | B6 374 | A0 375 | B2 376 | CE 377 | 88 378 | C4 379 | FF 380 | FF 381 | CD 382 | F0 383 | 00 384 | 00 385 | CD 386 | 00 387 | 55 388 | 70 389 | B3 390 | 68 391 | FA 392 | D1 393 | 15 394 | 19 395 | FF 396 | E4 397 | E4 398 | DE 399 | 00 400 | F5 401 | 80 402 | 8E 403 | A5 404 | 45 405 | 70 406 | E8 407 | FB 408 | EE 409 | 70 410 | EF 411 | DA 412 | 85 413 | C0 414 | A0 415 | E0 416 | 00 417 | 00 418 | 8F 419 | 69 420 | 45 421 | 80 422 | A4 423 | 8B 424 | F5 425 | 52 426 | C0 427 | CE 428 | 5A 429 | 80 430 | FA 431 | FF 432 | 82 433 | B4 434 | 80 435 | BF 436 | 63 437 | E0 438 | 82 439 | DE 440 | FF 441 | F5 442 | FF 443 | CD 444 | |
445 |
446 | Blue
447 | FF 448 | D7 449 | FF 450 | D4 451 | FF 452 | DC 453 | C4 454 | 00 455 | CD 456 | FF 457 | E2 458 | 2A 459 | 87 460 | A0 461 | 00 462 | 1E 463 | 50 464 | ED 465 | DC 466 | 3C 467 | FF 468 | 8B 469 | 8B 470 | 0B 471 | A9 472 | 00 473 | 6B 474 | 8B 475 | 2F 476 | 00 477 | CC 478 | 00 479 | 7A 480 | 8F 481 | 8B 482 | 4F 483 | D1 484 | D3 485 | 93 486 | FF 487 | 69 488 | FF 489 | 22 490 | F0 491 | 22 492 | FF 493 | DC 494 | FF 495 | 00 496 | 20 497 | 80 498 | 00 499 | 2F 500 | F0 501 | B4 502 | 5C 503 | 82 504 | F0 505 | 8C 506 | FA 507 | F5 508 | 00 509 | CD 510 | E6 511 | 80 512 | FF 513 | D2 514 | 90 515 | D3 516 | C1 517 | 7A 518 | AA 519 | FA 520 | 99 521 | DE 522 | E0 523 | 00 524 | 32 525 | E6 526 | FF 527 | 00 528 | AA 529 | CD 530 | D3 531 | DB 532 | 71 533 | EE 534 | 9A 535 | CC 536 | 85 537 | 70 538 | FA 539 | E1 540 | B5 541 | AD 542 | 80 543 | E6 544 | 00 545 | 23 546 | 00 547 | 00 548 | D6 549 | AA 550 | 98 551 | EE 552 | 93 553 | D5 554 | B9 555 | 3F 556 | CB 557 | DD 558 | E6 559 | 80 560 | 00 561 | 8F 562 | E1 563 | 13 564 | 72 565 | 60 566 | 57 567 | EE 568 | 2D 569 | C0 570 | EB 571 | CD 572 | 90 573 | FA 574 | 7F 575 | B4 576 | 8C 577 | 80 578 | D8 579 | 47 580 | D0 581 | EE 582 | B3 583 | FF 584 | F5 585 | 00 586 | 32 587 | |
588 |
35 | 36 |
49 | This simple mechanism provides a powerful new tool which enables a host 50 | of new types of applications to be written for web-based environments. 51 | Shopping applications can now store information about the currently 52 | selected items, for fee services can send back registration information 53 | and free the client from retyping a user-id on next connection, 54 | sites can store per-user preferences on the client, and have the client supply 55 | those preferences every time that site is connected to. 56 | 57 |
73 | Set-Cookie: NAME=VALUE; expires=DATE; 74 | path=PATH; domain=DOMAIN_NAME; secure 75 |76 |
This is the only required attribute 82 | on the Set-Cookie header.
83 |
89 | The date string is formatted as: 90 |
Wdy, DD-Mon-YYYY HH:MM:SS GMT91 | This is based on 92 | RFC 822, 93 | RFC 850, 94 | 95 | RFC 1036, and 96 | 97 | RFC 1123, 98 | with the variations that the only legal time zone is GMT and 99 | the separators between the elements of the date must be dashes. 100 |
101 | expires is an optional attribute. If not specified, the cookie will 102 | expire when the user's session ends.
103 | Note: There is a bug in Netscape Navigator version 1.1 and earlier. 104 | Only cookies whose path attribute is set explicitly to "/" will 105 | be properly saved between sessions if they have an expires 106 | attribute.
107 | 108 |
119 | 120 | Only hosts within the specified domain 121 | can set a cookie for a domain and domains must have at least two (2) 122 | or three (3) periods in them to prevent domains of the form: 123 | ".com", ".edu", and "va.us". Any domain that fails within 124 | one of the seven special top level domains listed below only require 125 | two periods. Any other domain requires at least three. The 126 | seven special top level domains are: "COM", "EDU", "NET", "ORG", 127 | "GOV", "MIL", and "INT". 128 | 129 |
130 | The default value of domain is the host name of the server 131 | which generated the cookie response.
132 |
143 | If the path is not specified, it as assumed to be the same path 144 | as the document being described by the header which contains the cookie. 145 |
146 |
152 | If secure is not specified, a cookie is considered safe to be sent 153 | in the clear over unsecured channels. 154 |
163 | Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ... 164 |165 | 166 |
172 |
176 |
181 |
187 |
193 |
213 |
Similarly, if a client request contains a Cookie: header, it 226 | should be forwarded through a proxy, even if the conditional 227 | If-modified-since request is being made. 228 |
242 | Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT243 |
Cookie: CUSTOMER=WILE_E_COYOTE245 |
Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/247 |
Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001249 |
Set-Cookie: SHIPPING=FEDEX; path=/foo251 |
Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001253 |
Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX255 |
259 |
Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/261 |
Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001263 |
Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo265 |
Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001267 |
274 | 275 |
277 |
278 |
279 | Corporate Sales: 415/937-2555; Personal Sales: 415/937-3777; Government Sales: 415/937-3678
281 | Copyright © 1997 Netscape Communications Corporation
282 | This site powered by Netscape SuiteSpot servers.
280 | If you have any questions, please visit Customer Service, or contact your nearest sales office.
286 | 287 | 288 | -------------------------------------------------------------------------------- /js-1.0/events.html: -------------------------------------------------------------------------------- 1 |
27 | An expression is any valid set of literals, variables, operators, 28 | and expressions that evaluates to a single value. 29 | The value may be a number, a string, or a logical value. 30 | 31 | Conceptually, there are two types of expressions: 32 | those that assign a value to a variable, and those that simply 33 | have a value. For example, the expression 34 |
35 | x = 7
36 |
37 | is an expression that assigns x the value 7. This expression itself 38 | evaluates to 7. Such expressions use assignment operators. 39 | On the other hand, the expression 40 |
41 | 3 + 4
42 |
43 | simply evaluates to 7; it does not perform an assignment. The 44 | operators used in such expressions are referred to simply as operators. 45 |
46 | JavaScript has the following kinds of expressions: 47 |
53 | The special keyword null denotes a null value. 54 | In contrast, variables that have not been assigned a value are undefined, and cannot 55 | be used without a run-time error. 56 | 57 |
59 | A conditional expression can have one of two values based on a condition. 60 | The syntax is 61 |
62 | (condition) ? val1 : val2 63 |64 |
65 | If condition is true, the expression has the value of val1, 66 | Otherwise it has the value of val2. 67 | You can use a conditional expression anywhere you would use a standard 68 | expression. 69 |
70 | For example, 71 |
72 | status = (age >= 18) ? "adult" : "minor" 73 |74 | This statement assigns the value "adult" to the variable status 75 | if age is eighteen or greater. Otherwise, it assigns the value 76 | "minor" to status. 77 | 78 |
81 | An assignment operator assigns a value to its left operand based 82 | on the value of its right operand. The basic assignment operator 83 | is equal (=), which assigns the value of its right operand to 84 | its left operand. That is, x = y assigns the value of y to x. 85 |
86 | The other operators are shorthand for standard arithmetic operations 87 | as follows: 88 |
97 | There are additional assignment operators for bitwise operations: 98 |
110 | JavaScript has arithmetic, string, and logical operators. There 111 | are both binary and unary operators. A binary operator 112 | requires two operands, one before the operator and one after the 113 | operator: 114 |
115 | operand1 operator operand2
116 |
117 | For example, 118 | 3 + 4 or x * y 119 |
120 | A unary operator requires a single operand, either before or after 121 | the operator: 122 |
123 | operator operand
124 |
125 | or 126 |
127 | operand operator
128 |
129 | For example x++ or ++x.
130 |
131 | Arithmetic Operators
132 |
133 |
134 | Arithmetic operators take numerical values (either literals or 135 | variables) as their operands and return a single numerical value. 136 | 137 |
140 | The standard arthmetic operators are addition (+), subtraction (-), multiplication (*), 141 | and division (/). These operators work in the standard way. 142 | 143 |
146 | The modulus operator is used as follows:
147 | var1 % var2
148 |
149 | The modulus operator returns the first operand modulo the second 150 | operand, that is, var1 modulo var2, in the statement 151 | above, where var1 and var2 are variables. The modulo 152 | function is the remainder of integrally dividing var1 by var2. 153 | For example, 12 % 5 returns 2. 154 | 155 |
158 | The increment operator is used as follows:
159 |
160 | var++
or ++var
161 |
162 | This operator increments (adds one to) its operand and returns a value. 163 | If used postfix, with operator after operand (for example x++), then it 164 | returns the value before incrementing. If used prefix with operator before 165 | operand (for example, ++x), then it returns the value after incrementing. 166 |
167 | For example, if x is 3, then the statement 168 |
169 | y = x++
170 |
171 | increments x to 4 and sets y to 3. 172 |
173 | If x is 3, then the statement 174 |
175 | y = ++x
176 |
177 | increments x to 4 and sets y to 4. 178 | 179 |
182 | The decrement operator is used as follows: 183 |
184 | var--
or --var
185 |
186 | This operator decrements (subtracts one from) its operand 187 | and returns a value. If used postfix (for example x--) then it 188 | returns the value before decrementing. If used prefix (for example, 189 | --x), then it returns the value after decrementing. 190 |
191 | For example, if x is 3, then the statement 192 |
193 | y = x--
194 |
195 | decrements x to 2 and sets y to 3. 196 |
197 | If x is 3, then the statement 198 |
199 | y = --x
200 |
201 | decrements x to 2 and sets y to 2. 202 | 203 |
206 | The unary negation operator must precede its operand. It negates 207 | its operand. For example, 208 |
209 | x = -x 210 |
211 | negates the value of x; that is if x were 3, it would become -3.
212 |
213 | Bitwise Operators
214 |
215 | Bitwise operators treat their operands as a set of bits (zeros and ones), 216 | rather than as decimal, hexadecimal, or octal numbers. 217 | For example, the decimal number 9 has a binary representation of 1001. 218 | Bitwise operators perform their operations on such binary representations, 219 | but they return standard JavaScript numerical values. 220 | 221 |
224 | The bitwise logical operators work conceptually as follows: 225 |
234 | The bitwise operators are: 235 |
241 | For example, the binary representation of 9 is 1001, and the binary representation 242 | of 15 is 1111. So, when the bitwise operators are applied to these values, 243 | the results are as follows: 244 |
252 | The bitwise shift operators are: 253 |
260 | The shift operators take two operands: the first is a quantity to be shifted, and the 261 | second specifies the number of bit positions by which the first operand is 262 | to be shifted. The direction of the shift operation is controlled by the operator used. 263 |
264 | Shift operators convert their operands to 32-bit integers, and return a result of 265 | the same type as the left operator. 266 | 267 |
269 | This operator shifts the first operand the specified number of bits to the left. 270 | Excess bits shifted off to the left are discarded. Zero bits are shifted in from 271 | the right. 272 |
273 | For example, 9<<2 yields 36, because 1001 shifted two bits to the left becomes 100100, 274 | which is 36. 275 | 276 |
278 | This operator shifts the first operand the specified number of bits to the right. 279 | Excess bits shifted off to the right are discarded. 280 | Copies of the leftmost bit are shifted in from the left. 281 |
282 | For example, 9>>2 yields 2, because 1001 shifted two bits to the right becomes 10, 283 | which is 2. Likewise, -9>>2 yields -3, because the sign is preserved. 284 | 285 |
287 | This operator shifts the first operand the specified number of bits to the left. 288 | Excess bits shifted off to the right are discarded. 289 | Zero bits are shifted in from the left. 290 | 291 |
292 | For example, 19>>>2 yields 4, because 10011 shifted two bits to the right becomes 100,
293 | which is 4. For postive numbers, zero-fill right shift and sign-propagating right
294 | shift yield the same result.
295 |
296 |
297 | Logical Operators
298 |
299 |
300 | Logical operators take logical (Boolean) values as operands. 301 | They return a logical value. Logical values are true and false. 302 | 303 |
306 | Usage: expr1 && expr2
307 |
308 | The logical "and" operator returns true if both logical 309 | expressions expr1 and expr2 are true. Otherwise, 310 | it returns false. 311 | 312 |
315 | Usage: expr1 || expr2
316 |
317 | The logical "or" operator returns true if either logical 318 | expression expr1 or expr2 is true. If both expr1 319 | and expr2 are false, then it returns false. 320 | 321 |
324 | Usage: !expr
325 |
326 | The logical "not" operator is a unary operator that 327 | negates its operand expression 328 | expr. That is, if expr is true, it returns false, 329 | and if expr is false, then it returns true. 330 | 331 |
333 | As logical expressions are evaluated left to right, they are tested for 334 | possible "short circuit" evaluation using the following rule: 335 |
340 | The rules of logic guarantee that these evaluations will always be correct.
341 | Note that the anything part of the above expressions is not evaluated, so any
342 | side effects of doing so do not take effect.
343 |
344 |
345 | Comparison Operators (= =, >, >=, <, <=, !=)
346 |
347 |
348 | A comparison operator compares its operands and returns a logical 349 | value based on whether the comparison is true or not. The operands 350 | may be numerical or string values. When used on string values, 351 | the comparisons are based on the standard lexicographical ordering. 352 |
353 | The operators are: 354 |
375 | In addition to the comparison operators, which may be used on 376 | string values, the concatenation operator (+) concatenates two 377 | string values together, returning another string that is the union 378 | of the two operand strings. For example, 379 |
380 | "my " + "string"
381 |
382 | returns the string 383 |
384 | "my string"
385 |
386 |
387 | The shorthand assignment operator += can also be used to concatenate strings. 388 | For example, if the variable mystring is a string that has the value 389 | "alpha", then the expression 390 |
mystring += "bet"391 | evaluates to "alphabet" and assigns this value to mystring. 392 | 393 |
396 | The precedence of operators determines the order they are 397 | applied when evaluating an expression. You can override operator 398 | precedence by using parentheses. 399 |
400 | The precedence of operators, from lowest to highest is as follows: 401 |
421 | 422 | 423 | 424 | -------------------------------------------------------------------------------- /js-1.0/ident.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |19 | JavaScript recognizes the following types of values: 20 |
27 | This relatively small set of types of values, or data types, 28 | enables you to perform useful functions with your applications. Notice that there 29 | is no explicit distinction between integer and real-valued numbers. Nor is there 30 | an explicit date data type in Navigator. However, the date object and related 31 | built-in functions enable you to handle dates. 32 |
33 | Objects and functions are the other fundamental elements in the language. 34 | You can think of objects as named containers for values, and functions as procedures 35 | that your application can perform. 36 | 37 |
39 | JavaScript is a loosely typed language. That means that you do not have to specify 40 | the datatype of a variable when you declare it, and datatypes are converted 41 | automatically as needed during the course of script execution. So, for example, you could define a variable as follows: 42 |
43 | var answer = 42 44 |45 |
46 | And later, you could assign the same variable a string value, for example: 47 |
48 | answer = "Thanks for all the fish..." 49 |50 |
51 | Because JavaScript is loosely typed, this will not cause an error message. 52 |
53 | In general, in expressions involving numeric and string values, JavaScript converts the numeric values to strings. 54 | For example, consider the following statements: 55 |
56 | x = "The answer is " + 42 57 | y = 42 + " is the answer." 58 |59 |
60 | The first statement will the string "The answer is 42". The second statement will return the string "42 is the answer". 61 | 62 |
63 | JavaScript provides several special functions for manipulating string and numeric values: 64 |
74 | You use variables to hold values in your application. You 75 | give these variables names by which you reference them, 76 | and there are certain rules to which the names must conform. 77 |
78 | A JavaScript identifier or name must start with a letter or 79 | underscore ("_"); subsequent characters can also be 80 | digits (0-9). Letters include the characters "A" through 81 | "Z" (uppercase) and the characters "a" through 82 | "z" (lowercase). 83 | JavaScript is case-sensitive. 84 |
85 | Some examples of legal names are: 86 |
Number_hits
88 | temp99
89 | _name
90 | 94 | The scope of a variable is where you can use it in a script. 95 | In JavaScript, there are two scopes that a variable can have: 96 |
102 | To declare a local variable inside a function, use the var keyword, 103 | for example: 104 |
105 | var total = 0 106 |107 |
108 | To declare a global variable, declare the variable by assignment, that is 109 | simply assign the desired value to the variable (either in a function or 110 | outside a function), for example: 111 |
112 | total = 0 113 |114 |
115 | It is good programming practice to 116 | declare global variables at the beginning of your script, so 117 | that functions will inherit the variable and its value. 118 | 119 | 120 |
124 | Literals are the way you represent values in JavaScript. 125 | These are fixed values that you literally provide in your 126 | application source, and are not variables. Examples of literals 127 | include: 128 |
42
130 | 3.14159
131 | "To be or not to be"
132 | 137 | Integers can be expressed in decimal (base 10), hexadecimal (base 138 | 16), or octal (base 8) format. A decimal integer literal consists 139 | of a sequence of digits (optionally suffixed as described below) 140 | without a leading 0 (zero). 141 |
142 | An integer can be expressed in octal or hexadecimal rather than 143 | decimal. A leading 0 (zero) on an integer literal means it is 144 | in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal 145 | integers can include digits (0-9) and the letters a-f and A-F. 146 | Octal integers can include only the digits 0-7. 147 | 148 |
151 | A floating point literal can have the following parts: a decimal 152 | integer, a decimal point ("."), a fraction (another 153 | decimal number), an exponent, and a type suffix. The exponent 154 | part is an "e" or "E" followed by an integer, which can be signed (preceded by 155 | a "+" or "-"). 156 | A floating point literal must have at least one digit, plus either 157 | a decimal point or "e" (or "E"). Some examples of floating point literals 158 | are: 159 |
169 | The boolean type has two literal values: true and false. 170 | 171 |
173 | A string literal is zero or more characters enclosed in double 174 | (") or single (') quotes. A string must be delimited by quotes 175 | of the same type; that is, either both single quotes or double 176 | quotes. The following are examples of string literals: 177 |
"blah"
179 | 'blah'
180 | "1234"
181 | "one line \n another line"
182 | 186 | You can use the following special characters in JavaScript string literals: 187 |
197 | You can insert quotes inside of strings by preceding them by a backslash. 198 | This is known as escaping the quotes. 199 | For example, 200 |
201 | var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service" 202 | document.write(quote) 203 |204 |
205 | The result of this would be
206 | 210 | 211 | -------------------------------------------------------------------------------- /js-1.0/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |9 | Development of the JavaScript language and its documentation 10 | continues. Additional features are planned; some current features 11 | could be modified if necessary. 12 |
24 |
25 | JavaScript is a compact, object-based scripting language for developing client and server 26 | Internet applications. Netscape Navigator 2.0 interprets JavaScript statements embedded 27 | directly in an HTML page, and LiveWire enables you to create server-based applications similar 28 | to common gateway interface (CGI) programs. 29 |
30 | In a client application for Navigator, JavaScript statements embedded in an HTML page can 31 | recognize and respond to user events such as mouse clicks, form input, and page navigation. 32 |
33 | For example, you can write a JavaScript function to verify that users enter valid 34 | information into a form requesting a telephone number or zip code. 35 | Without any network transmission, an HTML page with embedded JavaScript can interpret 36 | the entered text and alert the user with a message dialog if the input is invalid. 37 | Or you can use JavaScript to perform an action (such as play an audio file, execute an applet, or communicate with a plug-in) in response to the user opening or exiting a page. 38 | 39 |
42 | The JavaScript language resembles Java, but without Java's static 43 | typing and strong type checking. JavaScript supports most of Java's 44 | expression syntax and basic control flow constructs. In contrast to 45 | Java's compile-time system of classes built by declarations, 46 | JavaScript supports a run-time system based on a small number of 47 | data types representing numeric, Boolean, and string values. 48 | JavaScript has a simple instance-based object model that still provides 49 | significant capabilities. 50 | 51 |
52 | JavaScript also supports functions, again without any special declarative 53 | requirements. Functions can be properties of 54 | objects, executing as loosely typed methods. 55 |
56 | JavaScript complements Java by exposing useful properties of Java 57 | applets to script authors. JavaScript statements can get and set exposed properties to query the 58 | state or alter the performance of an applet or plug-in. 59 |
60 | Java is an extension language designed, in particular, for fast
61 | execution and type safety. Type safety is reflected by being unable
62 | to cast a Java int
into an object reference or to get at
63 | private memory by corrupting Java bytecodes.
64 |
65 | Java programs consist exclusively of classes and their methods. Java's 66 | requirements for declaring classes, writing methods, and ensuring 67 | type safety make programming more complex than JavaScript authoring. 68 | Java's inheritance and strong typing also tend to require tightly 69 | coupled object hierarchies. 70 |
71 | In contrast, JavaScript descends in spirit from a line of smaller, 72 | dynamically typed languages like HyperTalk and dBASE. These scripting 73 | languages offer programming tools to a much wider audience because of 74 | their easier syntax, specialized built-in functionality, and minimal 75 | requirements for object creation. 76 |
77 | The following table compares and contrasts JavaScript and Java. 78 |
79 |
JavaScript | 82 |Java | 83 |
---|---|
Interpreted (not compiled) by client. 87 | | Compiled on server before execution on client. 88 | |
Object-based. Code uses built-in, extensible objects, but no classes or 92 | inheritance. 93 | | Object-oriented. Applets consist of object classes with inheritance. 94 | |
Code integrated with, and embedded in, HTML. 98 | | Applets distinct from HTML (accessed from HTML pages). 99 | |
Variable data types not declared (loose typing). 103 | | Variable data types must be declared (strong typing). 104 | |
Dynamic binding. Object references checked at run-time. 108 | | Static binding. Object references must exist at compile-time. 109 | |
Cannot automatically write to hard disk. 113 | | Cannot automatically write to hard disk. 114 | |
5 | Reserved words
6 |
7 |
The reserved words in this list cannot be used as JavaScript variables, functions, methods, or object names. Some of these words are keywords used in JavaScript; others are reserved for future use. 8 |
11 | |
26 |
27 | |
41 |
42 | |
56 |
57 | |
71 |
JavaScript is based on a simple object-oriented paradigm. 7 | An object is a construct with properties that are JavaScript variables. 8 | Properties can be other objects. Functions associated with an object are known as the 9 | object's methods. 10 |
11 | In addition to objects that are built into the Navigator client and the LiveWire server, 12 | you can define your own objects. 13 | 14 |
A JavaScript object has properties associated with it. You access the properties of an 23 | object with a simple notation: 24 |
25 | objectName.propertyName 26 |27 |
28 | Both the object name and property name are case sensitive. 29 | You define a property by assigning it a value. For example, suppose there is an object 30 | named myCar (we'll discuss how to create objects later-for now, just assume the object 31 | already exists). You can give it properties named make, model, 32 | and year as follows: 33 |
34 | myCar.make = "Ford" 35 | myCar.model = "Mustang" 36 | myCar.year = 69; 37 |38 |
39 | You can also refer to these properties using an array notation as follows: 40 |
41 | mycar["make"] = "Ford 42 | myCar["model"] = "Mustang" 43 | myCar["year"] = 69; 44 |45 |
This type of an array is known as an associative array, because each 46 | index element is also associated with a string value. To illustrate how this works, 47 | the following function displays the properties of the object, when you 48 | pass the object and the object's name as arguments to the function: 49 |
function show_props(obj, obj_name) { 50 | var result = "" 51 | for (var i in obj) 52 | result += obj_name + "." + i + " = " + obj[i] + "\n" 53 | return result; 54 | }55 | 56 |
So, the function call show_props(myCar, "myCar") would 57 | return the following: 58 |
myCar.make = Ford 59 | myCar.model = Mustang 60 | myCar.year = 6761 |
62 | You may also define properties using ordinal numbers, for example: 63 |
64 | temp[0] = 34 65 | temp[1] = 42 66 | temp[2] = 56 67 |68 | These statements create three properties of the object temp, and you must refer 69 | to these properties as temp[i], where i is an integer between 0 and 2. 70 | 71 | 72 | 73 |
75 | Functions are one of the fundamental building blocks in JavaScript. 76 | A function is a JavaScript procedure--a set of statments that performs a specific task. 77 |
78 | A function definition consists of the function keyword, followed by 79 |
85 | In a Navigator application, you can use any functions defined in the current page.
86 | It is generally a good idea to define all your functions in the HEAD
of a page.
87 | When a user loads the page, the functions will then be loaded first.
88 |
89 |
90 | The statements in a function can include other function calls defined for the 91 | current application. 92 | 93 |
94 | For example, here is the definition of a simple function named pretty_print: 95 |
" + string) 98 | } 99 |
101 | This function takes a string as its argument, adds some HTML tags to it using the concatenation 102 | operator (+), then displays the result to the current document. 103 |
104 | Defining a function does not execute it. 105 | You have to call the function for it to do its work. 106 | For example, you could call the pretty_print function as follows: 107 |
113 | The arguments of a function are not limited to just strings and numbers. 114 | You can pass whole objects to a function, too. 115 | The show_props function from the previous section is an example of a function that takes 116 | an object as an argument. 117 |
118 | The arguments of a function are maintained in an array. 119 | Within a function, you can address the parameters passed to it as follows: 120 |
121 | functionName.arguments[i] 122 |123 | where functionName is the name of the function and i is the ordinal 124 | number of the argument, starting at zero. So, the first argument passed to a function named 125 | myfunc would be myfunc.arguments[0]. The total number of arguments is indicated by the variable 126 | arguments.length. 127 | 128 |
129 | A function can even be recursive, that is, it can call itself. For example, here is a function that computes factorials: 130 |
131 | function factorial(n) { 132 | if ((n == 0) || (n == 1)) 133 | return 1 134 | else { 135 | result = (n * factorial(n-1) ) 136 | return result 137 | } 138 | } 139 |140 | 141 |
142 | You could then display the factorials of one through five as follows: 143 |
144 | for (x = 0; x < 5; x++) { 145 | document.write(x, " factorial is ", factorial(x)) 146 | document.write("149 |
") 147 | } 148 |
150 | The results would be:
151 |
152 | 0 factorial is 1
153 |
1 factorial is 1
154 |
2 factorial is 2
155 |
3 factorial is 6
156 |
4 factorial is 24
157 |
5 factorial is 120
158 |
159 |
161 | You can call a function with more arguments than it is formally declared to accept 162 | using the arguments array. This is often useful if you don't know beforehand 163 | how many arguments will be passed to the function. 164 | You can use arguments.length to determine the number of arguments actually passed to the function,a 165 | and then treat each argument using the arguments array. 166 |
167 | For example, consider a function defined to create HTML lists. 168 | The only formal argument for the function is a string that is "U" if the list is to be unordered 169 | (bulleted) or "O" if the list is to be ordered (numbered). The function is defined as follows: 170 |
179 | You can pass any number of arguments to this function and it will then display each argument as 180 | an item in the indicated type of list. For example, the following call to the function: 181 |
182 | list("o", "one", 1967, "three", "etc, etc...") 183 |184 |
185 | results in this output: 186 | 195 | 196 | 197 | 198 |
200 | A method is a function associated with an object. You define a method in the same way 201 | as you define a standard function. Then, use the following syntax to associate the function 202 | with an existing object: 203 |
204 | object.methodname = function_name 205 |206 | where object is an existing object, methodname is the name you are assigning to 207 | the method, and function_name is the name of the function. 208 |
209 | You can then call the method in the context of the object as follows: 210 |
211 | object.methodname(params); 212 |213 | 214 |
216 | JavaScript has a special keyword, this, that you can 217 | use to refer to the current object. For example, suppose you have a function called 218 | validate that validates an object's value property, given the object, and the 219 | high and low values: 220 |
221 | function validate(obj, lowval, hival) { 222 | if ((obj.value < lowval) || (obj.value > hival)) 223 | alert("Invalid Value!") 224 | } 225 |226 |
227 | Then, you could call validate in each form element's 228 | onChange event handler, using this to pass it the form element, as in the 229 | following example: 230 | 231 |
236 | In general, in a method this refers to the calling object.
237 |
238 |
239 | Creating New Objects
240 |
241 | Both client and server JavaScript have a number of predefined objects. 242 | In addition, you can create your own objects. 243 | Creating your own object requires two steps: 244 |
249 | To define an object type, create a function for the object 250 | type that specifies its name, and its properties and methods. 251 | 252 | For example, suppose you want to create an object type for cars. 253 | You want this type of object to be called car, and you want it 254 | to have properties for make, model, year, and color. 255 | To do this, you would write the following function: 256 |
257 | function car(make, model, year) { 258 | this.make = make; 259 | this.model = model; 260 | this.year = year; 261 | } 262 |263 |
264 | Notice the use of this to assign values to the object's properties based 265 | on the values passed to the function. 266 |
267 | Now you can create an object called mycar as follows: 268 |
269 | mycar = new car("Eagle", "Talon TSi", 1993); 270 |271 |
272 | This statement creates mycar and assigns it the specified values for its properties.
273 | Then the value of mycar.make
is the string "Eagle", mycar.year
274 | is the integer 1993, and so on.
275 |
276 | You can create any number of car objects by calls to new.
277 | For example,
278 |
279 | kenscar = new car("Nissan", "300ZX", 1992)
280 |
281 |
282 | An object can have a property that is itself another object. 283 | For example, suppose I define an object called person as follows: 284 |
285 | function person(name, age, sex) { 286 | this.name = name; 287 | this.age = age; 288 | this.sex = sex; 289 | } 290 |291 |
292 | And then instantiate two new person objects as follows: 293 |
294 | rand = new person("Rand McNally", 33, "M") 295 | ken = new person("Ken Jones", 39, "M") 296 |297 |
298 | Then we can rewrite the definition of car to include an owner property 299 | that takes a person object, as follows: 300 |
301 | function car(make, model, year, owner) { 302 | this.make = make; 303 | this.model = model; 304 | this.year = year; 305 | this.owner = owner; 306 | } 307 |308 |
309 | To instantiate the new objects, you then use the following: 310 |
311 | car1 = new car("Eagle", "Talon TSi", 1993, rand); 312 | car2 = new car("Nissan", "300ZX", 1992, ken) 313 |314 |
315 | Notice that instead of passing a literal string or integer value when creating the 316 | new objects, the above statements pass the objects rand and ken as the arguments for the owners. 317 | Then if you want to find out the name of the owner of car2, 318 | you can access the following property: 319 |
320 | car2.owner.name 321 |322 |
323 | Note that you can always add a property to a previously defined object. 324 | For example, the statement: 325 |
326 | car1.color = "black" 327 |328 | adds a property color to car1, and assigns it a value of "black". 329 | However, this does not affect any other objects. 330 | To add the new property to all objects of the same type, you have to add the property to the 331 | definition of the car object type. 332 | 333 |
335 | You can define methods for an object type by including a method defnition in the 336 | object type definition. For example, suppose you have a set of image GIF files, 337 | and you want to define a method that displays the information for the cars, along with the 338 | corresponding image. You could define a function such as: 339 |
340 | function displayCar() { 341 | var result = "A Beautiful " + this.year 342 | + " " + this.make + " " + this.model; 343 | pretty_print(result) 344 | } 345 |346 | where pretty_print is the previously defined function to display a string. 347 | Notice the use of this to refer to the object to which the method belongs. 348 |
349 | You can make this function a method of car by adding the statement 350 |
this.displayCar = displayCar;351 | to the object definition. So, the full definition of car would now 352 | look like: 353 | 354 |
355 | function car(make, model, year, owner) { 356 | this.make = make; 357 | this.model = model; 358 | this.year = year; 359 | this.owner = owner; 360 | this.displayCar = displayCar; 361 | } 362 |363 |
364 | Then you can call this new method as follows: 365 |
366 | car1.displayCar() 367 | car2.displayCar() 368 |369 | This will produce output like this: 370 |
371 |
374 |
16 | When you load a page in Navigator, it creates a number of objects corresponding 17 | to the page, its contents, and other pertinent information. 18 |
19 | Every page always has the following objects: 20 |
31 | The properties of the document object are largely content-dependent. 32 | That is, they are created based on the content that you put in the document. 33 | For example, the document object has a property for each form and each anchor 34 | in the document. 35 |
36 | For example, suppose you create a page named simple.html that contains the following 37 | HTML: 38 |
48 | As always, there would be window, location, history, and document objects. 49 | These would have properties such as: 50 |
58 | These are just some example values. In practice, these values would be based 59 | on the document's actual location, its title, foreground and background colors, 60 | and so on. 61 |
62 | Navigator would also create the following objects based on the contents of the page: 63 |
69 | These would have properties such as: 70 |
83 | Notice that each of the property references above starts with "document," 84 | followed by the name of the form, "myform," and then the property name (for form properties) 85 | or the name of the form element. 86 | This sequence follows the Navigator's object hierarchy, discussed in the next section. 87 | 88 | 89 |
92 | The objects in Navigator exist in a hierarchy that reflects the 93 | hierarchical structure of the HTML page itself. 94 | Although you cannot derive object classes from these objects, 95 | as you can in languages such as Java, it is useful to understand the Navigator's 96 | JavaScript object hierarchy. 97 | In the strict object-oriented sense, this type of hierarchy is known 98 | as an instance hierarchy, since it concerns specific instances of 99 | objects rather than object classes. 100 |
101 | In this hierarchy, an object's "descendants" are properties of the object. 102 | For example, a form named "form1" is an object, but is also a property of document, 103 | and is referred to as "document.form1". 104 | 105 | The Navigator object hierarchy is illustrated below: 106 | 107 |
130 | To refer to specific properties of these objects, you must specify the object 131 | name and all its ancestors. 132 | Exception: You are not required to include the window object. 133 | 134 | 135 |
138 | To use JavaScript properly in the Navigator, it is important to have a basic understanding of how 139 | the Navigator performs layout. Layout refers to transforming the plain text directives 140 | of HTML into graphical display on your computer. Generally speaking, layout happens sequentially 141 | in the Navigator. That is, the Navigator starts from the top of the HTML file and works its way 142 | down, figuring out how to display output to the screen as it goes. So, it starts with the HEAD of 143 | an HTML document, then starts at the top of the BODY and works its way down. 144 |
145 | Because of this "top-down" behavior, JavaScript only reflects HTML that it has encountered. 146 | For example, suppose you define a form with a couple of text input elments: 147 |
220 | There would be a JavaScript object named myform based on this form. 221 | The form would have a property corresponding to the text object, 222 | that you would refer to as 223 | 224 |
235 | The forms in a document are stored in an array called forms. 236 | The first (topmost in the page) form is forms[0], 237 | the second forms[1], and so on. 238 | So the above references could also be: 239 |
244 | Likewise, the elements in a form, such as text fields, radio buttons, and so on, 245 | are stored in an elements array. 246 | 247 | 253 | 254 | 255 |
257 | The window object is the "parent" object for all other objects in Navigator. 258 | You can always omit the object name in references to window properties and methods. 259 | 260 |
261 | Window has several very useful methods that create new windows and 262 | pop-up dialog boxes: 263 |
270 | The window object has properties for all the frames in a frameset. 271 | The frames are stored in the frames array. 272 | The frames array contains an entry for each child frame in a window. For example, if a 273 | window contains three child frames, these frames are reflected as window.frames[0], 274 | window.frames[1], and window.frames[2]. 275 | 276 |
277 | The status property enables you to set the message in the status bar 278 | at the bottom of the client window. 279 | 280 | 281 | 282 | -------------------------------------------------------------------------------- /js-1.0/objects.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |4 |
Lets you work with dates and times. 6 | 7 |
To create a Date object: 9 |
10 | 1. dateObjectName = new Date() 11 | 2. dateObjectName = new Date("month day, year hours:minutes:seconds") 12 | 3. dateObjectName = new Date(year, month, day) 13 | 4. dateObjectName = new Date(year, month, day, hours, minutes, seconds) 14 |15 | dateObjectName is either the name of a new object or a property of an existing object. 16 |
To use Date methods: 19 |
20 | dateObjectName.methodName(parameters) 21 |22 | dateObjectName is either the name of an existing Date object or a property of an existing object.. 23 |
Exceptions: The Date object's parse and UTC methods are static methods that you use as follows: 26 |
27 | Date.UTC(parameters) 28 | Date.parse(parameters) 29 |30 | 31 | 32 |
The Date object is a built-in JavaScript object. 38 | 39 |
Form 1 of the syntax creates today's date and time. If you omit hours, minutes, or seconds from form 2 or 4 of the syntax, the value will be set to zero. 40 | 41 |
The way JavaScript handles dates is very similar to the way Java handles dates: both languages have many of the same date methods, and both store dates internally as the number of milliseconds since January 1, 1970 00:00:00. Dates prior to 1970 are not allowed. 42 | 43 | 44 |
77 |
88 | A Boolean value indicating the default selection state of a checkbox or radio button. 89 | 90 |
92 | 1. checkboxName.defaultChecked 93 | 2. radioName[index].defaultChecked 94 |95 |
checkboxName is either the value of the NAME attribute of a checkbox object or an element in the elements array.
96 |
radioName is the value of the NAME attribute of a radio object.
97 |
index is an integer representing a radio button in a radio object.
98 |
99 |
If an checkbox or radio button is selected by default, the value of the defaultChecked property is true; otherwise, it is false. defaultChecked initially reflects whether the CHECKED attribute is used within an <INPUT> tag; however, setting defaultChecked overrides the CHECKED attribute. 104 | 105 |
You can set the defaultChecked property at any time. The display of the checkbox or radio button does not update when you set the defaultChecked property, only when you set the checked property. 106 | 107 |
The following example resets an array of radio buttons called musicType on the musicForm form to the default selection state. 109 | 110 |
111 | function radioResetter() { 112 | var i="" 113 | for (i in document.musicForm.musicType) { 114 | if (document.musicForm.musicType[i].defaultChecked==true) { 115 | document.musicForm.musicType[i].checked=true 116 | } 117 | } 118 | } 119 |120 | 121 |
127 | A Boolean value indicating the default selection state of an option in a select object. 128 | 129 |
selectName.options[index].defaultSelected131 |
selectName is either the value of the NAME attribute of a select object or an element in the elements array.
132 |
index is an integer representing an option in a select object.
133 |
134 |
options array 136 | 137 |
If an option in a select object is selected by default, the value of the defaultSelected property is true; otherwise, it is false. defaultSelected initially reflects whether the SELECTED attribute is used within an <OPTION> tag; however, setting defaultSelected overrides the SELECTED attribute. 139 | 140 |
You can set the defaultSelected property at any time. The display of the select object does not update when you set the defaultSelected property, only when you set the selected or selectedIndex properties. 141 | 142 |
A select object created without the MULTIPLE attribute can have only one option selected by default. When you set defaultSelected in such an object, any previous default selections, including defaults set with the SELECTED attribute, are cleared. If you set defaultSelected in a select object created with the MULTIPLE attribute, previous default selections are not affected. 143 | 144 |
In the following example, the restoreDefault() function returns the musicType select object to its default state. The for loop uses the options array to evaluate every option in the select object. The if statement sets the selected property if defaultSelected is true. 146 |
147 | function restoreDefault() { 148 | for (var i = 0; i < document.musicForm.musicType.length; i++) { 149 | if (document.musicForm.musicType.options[i].defaultSelected == true) { 150 | document.musicForm.musicType.options[i].selected=true 151 | } 152 | } 153 | } 154 |155 | The previous example assumes that the select object is similar to the following: 156 |
157 | <SELECT NAME="musicType"> 158 | <OPTION SELECTED> R&B 159 | <OPTION> Jazz 160 | <OPTION> Blues 161 | <OPTION> New Age 162 | </SELECT> 163 |164 | 165 |
171 | The default message displayed in the status bar at the bottom of the window. 172 | 173 |
windowReference.defaultStatus175 |
windowReference is a valid way of referring to a window, as described in the window object. 176 | 177 |
window 179 | 180 |
The defaultStatus message appears when nothing else is in the status bar. Do not confuse the defaultStatus property with the status property. The status property reflects a priority or transient message in the status bar, such as the message that appears when a mouseOver event occurs over an anchor. 182 | 183 |
You can set the defaultStatus property at any time. You must return true if you want to set the defaultStatus property in the onMouseOver event handler. 184 | 185 | 186 |
In the following example, the statusSetter() function sets both the status and defaultStatus properties in an onMouseOver event handler: 188 | 189 |
190 | function statusSetter() { 191 | window.defaultStatus = "Click the link for the Netscape home page" 192 | window.status = "Netscape home page" 193 | } 194 | 195 | <A HREF="http://www.netscape.com" 196 | onMouseOver = "statusSetter(); return true">Netscape</A> 197 |198 | 199 | In the previous example, notice that the onMouseOver event handler returns a value of true. You must return true to set status or defaultStatus in an event handler. 200 | 201 |
207 | A string indicating the default value of a password, text, or textarea object. 208 | 209 |
211 | 1. passwordName.defaultValue 212 | 2. textName.defaultValue 213 | 3. textareaName.defaultValue 214 |215 |
passwordName is either the value of the NAME attribute of a password object or an element in the elements array.
216 |
textName is either the value of the NAME attribute of a text object or an element in the elements array.
217 |
textareaName is either the value of the NAME attribute of a textarea object or an element in the elements array.
218 |
219 |
password, text, textarea 221 | 222 |
The initial value of defaultValue differs for each object: 224 |
Setting defaultValue programatically overrides the initial setting. If you programatically set defaultValue for the password object and then evaluate it, JavaScript returns the current value. 229 | 230 |
You can set the defaultValue property at any time. The display of the related object does not update when you set the defaultValue property, only when you set the value property. 231 | 232 |
The following function evaluates the defaultValue property of objects on the surfCity form and displays the values in the msgWindow window: 234 |
235 | function defaultGetter() { 236 | msgWindow=window.open("") 237 | msgWindow.document.write("hidden.defaultValue is " + 238 | document.surfCity.hiddenObj.defaultValue + "<BR>") 239 | msgWindow.document.write("password.defaultValue is " + 240 | document.surfCity.passwordObj.defaultValue + "<BR>") 241 | msgWindow.document.write("text.defaultValue is " + 242 | document.surfCity.textObj.defaultValue + "<BR>") 243 | msgWindow.document.write("textarea.defaultValue is " + 244 | document.surfCity.textareaObj.defaultValue + "<BR>") 245 | msgWindow.document.close() 246 | } 247 |248 | 249 |
Contains information on the current document, and provides methods for displaying HTML output to the user. 255 | 256 | 257 |
To define a document object, use standard HTML syntax: 259 |
260 | <BODY 261 | BACKGROUND="backgroundImage" 262 | BGCOLOR="backgroundColor" 263 | TEXT="foregroundColor" 264 | LINK="unfollowedLinkColor" 265 | ALINK="activatedLinkColor" 266 | VLINK="followedLinkColor" 267 | [onLoad="handlerText"] 268 | [onUnload="handlerText"]> 269 | </BODY> 270 |271 | BACKGROUND specifies an image that fills the background of the document. 272 |
To use a document object's properties and methods: 275 |
276 | 1. document.propertyName 277 | 2. document.methodName(parameters) 278 |279 | propertyName is one of the properties listed below. 280 |
An HTML document consists of a <HEAD> and <BODY> tag. The <HEAD> includes information on the document's title and base (the absolute URL base to be used for relative URL links in the document). The <BODY> tag encloses the body of a document, which is defined by the current URL. The entire body of the document (all other HTML elements for the document) goes within the <BODY> tag. 289 | 290 |
You can load a new document by using the location object. 291 | 292 |
You can reference the anchors, forms, and links of a document by using the anchors, forms, and links arrays. These arrays contain an entry for each anchor, form, or link in a document. 293 | 294 | 295 |
The following objects are also properties of the document object: 311 |
The following example creates two frames, each with one document. The document in the first frame contains links to anchors in the document of the second frame. Each document defines its colors. 331 | 332 |
DOC0.HTML, which defines the frames, contains the following code: 333 |
DOC1.HTML, which defines the content for the first frame, contains the following code: 346 |
Some links 357 |
DOC2.HTML, which defines the content for the second frame, contains the following code: 366 |
Some numbers 374 |
Some colors 384 |
Some music types 393 |
Some countries 402 |
425 | Euler's constant and the base of natural logarithms, approximately 2.718. 426 | 427 |
Math.E429 | 430 |
Math 432 | 433 |
Because E is a constant, it is a read-only property of Math. 435 | 436 |
The following example displays Euler's constant: 438 |
document.write("Euler's constant is " + Math.E)439 | 440 |
An array of objects corresponding to form elements (such as checkbox, radio, and text objects) in source order. 446 | 447 |
449 | 1. formName.elements[index] 450 | 2. formName.elements.length 451 |452 |
formName is either the name of a form or an element in the forms array.
453 |
index is an integer representing an object on a form.
454 |
455 |
456 |
You can reference a form's elements in your code by using the elements array. This array contains an entry for each object (button, checkbox, hidden, password, radio, reset, select, submit, text, or textarea object) in a form in source order. For example, if a form has a text field and two checkboxes, these elements are reflected as formName.elements[0], formName.elements[1], and formName.elements[2]. 462 | 463 |
Although you can also reference a form's elements by using the element's name (from the NAME attribute), the elements array provides a way to reference form objects programatically without using their names. For example, if the first object on the userInfo form is the userName text object, you can evaluate it in either of the following ways: 464 |
465 | userInfo.userName.value 466 | userInfo.elements[0].value 467 |468 | 469 |
To obtain the number of elements on a form, use the length property: formName.elements.length. Each radio button in a radio object appears as a separate element in the elements array. 470 | 471 |
Elements in the elements array are read-only. For example, the statement formName.elements[0]="music" has no effect. 472 | 473 |
The value of each element in the elements array is the full HTML statement for the object. 474 | 475 | 476 |
See the examples for the name property. 482 | 483 | 484 |
An array of objects corresponding to form elements (such as checkbox, radio, and text objects) in source order. See elements array. 492 | 493 | 494 |
497 | A string specifying the MIME encoding of the form. 498 | 499 |
formName.encoding501 |
formName is either the name of a form or an element in the forms array. 502 | 503 |
form 505 | 506 |
The encoding property initially reflects the ENCTYPE attribute of the <FORM> tag; however, setting encoding overrides the ENCTYPE attribute. 508 | 509 |
You can set the encoding property at any time. 510 | 511 |
Certain values of the encoding property may require specific values for other form properties. See RFC 1867 for more information. 512 | 513 |
The following function returns the value of the musicForm encoding property: 515 |
516 | function getEncoding() { 517 | return document.musicForm.encoding 518 | } 519 |520 | 521 |
528 | Returns the ASCII encoding of an argument in the ISO Latin-1 character set. 529 | 530 |
escape("string")532 |
string is a non-alphanumeric string in the ISO Latin-1 character set, or a property of an existing object. 533 | 534 |
The escape function is not a method associated with any object, but is part of the language itself. 536 | 537 |
The value returned by the escape function is a string of the form "%xx", where xx is the ASCII encoding of a character in the argument. If you pass the escape function an alphanumeric character, the escape function returns the same character. 538 | 539 | 540 |
The following example returns "%26" 542 |
543 | escape("&") 544 |545 | 546 |
The following example returns "%21%23" 547 |
548 | escape("!#") 549 |550 | 551 |
558 | The eval function evaluates a string and returns a value. 559 | 560 |
eval(string)562 | string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. 563 | 564 |
The eval function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself. 566 | 567 |
The argument of the eval function is a string. Do not call eval to evaluate an arithmetic expression. JavaScript evaluates arithmetic expressions automatically. If the argument represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. 568 | 569 |
If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script. 570 | 571 |
Example 1. Both of the write statements below display 42. The first evaluates the string "x + y + 1", and the second evaluates the string "42". 573 | 574 |
575 | var x = 2 576 | var y = 39 577 | var z = "42" 578 | document.write(eval("x + y + 1"), "<BR>") 579 | document.write(eval(z), "<BR>") 580 |581 | 582 |
Example 2. In the following example, the getFieldName(n) function returns the name of the nth form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element. 583 |
584 | var field = getFieldName(3) 585 | document.write("The field named ", field, " has value of ", eval(field + ".value")) 586 |587 | 588 |
Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that opens an alert dialog box and assigns z a value of 42 if x is five, and assigns zero to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z. 589 | 590 |
591 | var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; " 592 | document.write("<P>z is ", eval(str)) 593 |594 | 595 |
Example 4. In the following example, the setValue() function uses eval to assign the value of the variable newValue to the text field textObject. 596 |
597 | function setValue (textObject, newValue) { 598 | eval ("document.forms[0]." + textObject + ".value") = newValue 599 | } 600 |601 | 602 | 603 |
606 | Returns enumber, where number is the argument, and e is Euler's constant, the base of the natural logarithms. 607 | 608 |
Math.exp(number)610 | number is any numeric expression or a property of an existing object. 611 | 612 |
Math 614 | 615 |
617 | //Displays the value 2.718281828459045 618 | document.write("The value of e<SUP>1</SUP> is " + Math.exp(1)) 619 |620 | 621 |
630 | 631 | -------------------------------------------------------------------------------- /js-1.0/ref_r-r.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |A set of radio buttons on an HTML form. A set of radio buttons lets the user choose one item from a list. 6 | 7 | 8 |
To define a set of radio buttons, use standard HTML syntax with the addition of the onClick event handler: 10 |
11 | <INPUT 12 | TYPE="radio" 13 | NAME="radioName" 14 | VALUE="buttonValue" 15 | [CHECKED] 16 | [onClick="handlerText"]> 17 | textToDisplay 18 |19 | NAME="radioName" specifies the name of the radio object. All radio buttons in a group have the same NAME attribute. You can access this value using the name property. 20 |
To use a radio button's properties and methods: 25 |
26 | 1. radioName[index1].propertyName 27 | 2. radioName[index1].methodName(parameters) 28 | 3. formName.elements[index2].propertyName 29 | 4. formName.elements[index2].methodName(parameters) 30 |31 | radioName is the value of the NAME attribute of a radio object. 32 |
A radio object on a form looks as follows: 45 |
50 |A radio object is a form element and must be defined within a <FORM> tag. 51 | 52 |
All radio buttons in a radio button group use the same name property. To access the individual radio buttons in your code, follow the object name with an index starting from zero, one for each button the same way you would for an array such as forms: document.forms[0].radioName[0] is the first, document.forms[0].radioName[1] is the second, etc. 53 | 54 | 55 |
Example 1. The following example defines a radio button group to choose among three music catalogs. Each radio button is given the same name, NAME="musicChoice", forming a group of buttons for which only one choice can be selected. The example also defines a text field that defaults to what was chosen via the radio buttons but that allows the user to type a nonstandard catalog name as well. The onClick event handler sets the catalog name input field when the user clicks a radio button. 73 |
Example 2. The following example contains a form with three text boxes and three radio buttons. The radio buttons let the user choose whether the text fields are converted to upper case or lower case, or not converted at all. Each text field has an onChange event handler that converts the field value depending on which radio button is checked. The radio buttons for upper case and lower case have onClick event handlers that convert all fields when the user clicks the radio button. 84 |
See also the example for the link object. 130 | 131 | 132 |
140 | Returns a pseudo-random number between zero and one. This method is available on Unix platforms only. 141 | 142 |
Math.random() 144 | 145 |
Math 147 | 148 |
150 | //Displays a random number between 0 and 1 151 | document.write("The random number is " + Math.random()) 152 |153 | 154 | 155 |
158 | Specifies the URL of the calling document when a user clicks a link. 159 | 160 |
document.referrer162 | 163 |
document 165 | 166 |
When a user navigates to a destination document by clicking a link object on a source document, the referrer property contains the URL of the source document. Evaluate the referrer property from the destination document. 168 | 169 |
referrer is a read-only property. 170 | 171 |
In the following example, the getReferrer() function is called from the destination document. It returns the URL of the source document. 173 |
174 | function getReferrer() { 175 | return document.referrer 176 | } 177 |178 | 179 | 180 |
A reset button on an HTML form. A reset button resets all elements in a form to their defaults. 183 | 184 | 185 |
To define a reset button, use standard HTML syntax with the addition of the onClick event handler: 187 |
188 | <INPUT 189 | TYPE="reset" 190 | NAME="resetName" 191 | VALUE="buttonText" 192 | [onClick="handlerText"]> 193 |194 | NAME="resetName" specifies the name of the reset object. You can access this value using the name property. 195 |
To use a reset object's properties and methods: 198 |
199 | 1. resetName.propertyName 200 | 2. resetName.methodName(parameters) 201 | 3. formName.elements[index].propertyName 202 | 4. formName.elements[index].methodName(parameters) 203 |204 | resetName is the value of the NAME attribute of a reset object. 205 |
A reset object on a form looks as follows: 217 |
220 |A reset object is a form element and must be defined within a <FORM> tag. 221 | 222 |
The reset button's onClick event handler cannot prevent a form from being reset; once the button is clicked, the reset cannot be canceled. 223 | 224 | 225 |
Example 1. The following example displays a text object with the default value "CA" and a reset button with the text "Clear Form" displayed on its face. If the user types a state abbreviation in the text object and then clicks the Clear Form button, the original value of "CA" is restored. 240 |
243 |
Example 2. The following example displays two text objects, a select object, and three radio buttons; all of these objects have default values. The form also has a reset button with the text "Defaults" on its face. If the user changes the value of any of the objects and then clicks the Defaults button, the original values are restored. 246 |
282 | Returns the value of a number rounded to the nearest integer. 283 | 284 |
Math.round(number)286 | number is any numeric expression or a property of an existing object. 287 | 288 |
Math 290 | 291 |
If the fractional portion of number is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number is less than .5, the argument is rounded to the next lowest integer. 293 | 294 |
296 | //Displays the value 20 297 | document.write("The rounded value is " + Math.round(20.49)) 298 | 299 | //Displays the value 21 300 | document.write("<P>The rounded value is " + Math.round(20.5)) 301 | 302 | //Displays the value -20 303 | document.write("<P>The rounded value is " + Math.round(-20.5)) 304 | 305 | //Displays the value -21 306 | document.write("<P>The rounded value is " + Math.round(-20.51)) 307 |308 | 309 | 310 |
316 | 317 | -------------------------------------------------------------------------------- /js-1.0/script.html: -------------------------------------------------------------------------------- 1 |
2 |17 | JavaScript can be embedded in an HTML document in two ways: 18 |
32 | The optional LANGUAGE attribute specifies the scripting language as 33 | follows: 34 |
41 | The HMTL tag, <SCRIPT>, and its closing counterpart, </SCRIPT> 42 | can enclose any number of JavaScript statements in a document. 43 |
44 | JavaScript is case sensitive. 45 |
48 | Example 1: a simple script. 49 |
50 |
63 | Example 1 page display. 64 |
65 | Hello net. That's all folks. 66 |
80 | Scripts placed within SCRIPT tags are evaluated after the page loads. 81 | Functions are stored, but not executed. Functions are executed by 82 | events in the page. 83 |
84 | It's important to understand the difference between defining a 85 | function and calling the function. Defining the function simply 86 | names the function and specifies what to do when the function is 87 | called. Calling the function actually performs the specified actions 88 | with the indicated parameters. 89 |
92 | Example 2: a script with a function and comments. 93 |
112 | Example 2 page display. 113 |
114 | We passed 5 to the function.
115 |
116 | The function returned 25.
117 |
118 | All done.
119 |
129 | Example 3: a script with two functions. 130 |
151 | Thanks. 152 | 153 |
156 | Example 3 results. 157 | 168 |
169 | Thanks. 170 |
186 | 187 | JavaScript applications in the Navigator are largely event-driven. 188 | Events are actions that occur, usually as a result of something the user does. 189 | For example, a button click is an event, as is giving focus to a form 190 | element. There is a specific set of events that Navigator recognizes. 191 | You can define Event handlers, scripts that are automatically executed 192 | when an event occurs. 193 | 194 |
195 | Event handlers are embedded in documents as attributes of HTML tags to which you 196 | assign JavaScript code to execute. 197 | The general syntax is 198 |
204 | For example, suppose you have created a JavaScript function called 205 | compute. You can cause Navigator to perform this function when the user clicks on a 206 | button by assigning the function call to the button's onClick event handler: 207 | 208 |
212 | You can put any JavaScript statements inside the quotes following onClick. 213 | These statements get executed when the user clicks on the button. 214 | If you want to include more than one statement, separate statements with a semicolon (;). 215 |
216 | In general, it is a good idea to define functions for your event handlers because: 217 |
224 | Notice in this example the use of this.form to refer to the current form. 225 | The keyword 226 | this refers to the current object-in the above example, 227 | the button object. The construct this.form then refers to the form 228 | containing the button. In the above example, the onClick event handler is a 229 | call to the compute() function, with this.form, the current form, 230 | as the parameter to the function. 231 | 232 |
233 | Events apply to HTML tags as follows: 234 |
242 | If an event applies to an HTML tag, then you can define an event handler for it. 243 | In general, an event handler has the name of the event, preceded by "on." 244 | For example, the event handler for the Focus event is onFocus. 245 |
246 | Many objects also have methods that emulate events. For example, button has a click 247 | method that emulates the button being clicked. 248 | Note: The event-emulation methods do not trigger event-handlers. So, for example, 249 | the click method does not trigger an onClick event-handler. 250 | However, you can always call an event-handler directly (for example, you can call 251 | onClick explicitly in a script). 252 | 253 |
254 |
Event | 257 |Occurs when... | 258 |Event Handler | 259 | 260 |
---|---|---|
blur | 262 |User removes input focus from form element | 263 |onBlur | 264 |
click | 268 |User clicks on form element or link | 269 |onClick | 270 |
change | 274 |User changes value of text, textarea, or select element | 275 |onChange | 276 |
focus | 280 |User gives form element input focus | 281 |onFocus | 282 |
load | 286 |User loads the page in the Navigator | 287 |onLoad | 288 |
mouseover | 292 |User moves mouse pointer over a link or anchor | 293 |onMouseOver | 294 |
select | 298 |User selects form element's input field | 299 |onSelect | 300 |
submit | 304 |User submits a form | 305 |onSubmit | 306 |
unload | 310 |User exits the page | 311 |onUnload | 312 |
318 | Example 4: a script with a form and an event handler attribute. 319 | 320 | 321 |
348 | Example 4 page display. 349 |
350 | 360 | 361 |
370 |373 | Example 5: a script with a form and event handler attribute within a BODY tag. 374 | 375 | 376 |
420 | Example 5 page display. 421 |
422 | Enter a number in the field and then click your mouse anywhere OUTSIDE of the field. 423 | Depending on what you enter, you will be prompted to enter another number, or thanked. 424 | 425 | 451 | 452 |
458 | 459 |463 | This section describes various useful scripting techniques. 464 |
465 |
467 | JavaScript in Navigator generates its results from the top of the page down. 468 | Once something has been formatted, you can't change it without reloading the page. 469 | Currently, you cannot update a particular part of a page without updating the entire page. 470 | However, you can update a "sub-window" in a frame separately. 471 |
472 |
474 | You cannot currently print output created with JavaScript. 475 | For example, if you had the following in a page: 476 |
This is some text. 478 | 479 |
483 |
485 | Be sure to alternate double quotes with single quotes. 486 | Since event handlers in HTML must be enclosed in quotes, you must use single 487 | quotes to delimit arguments. For example 488 | 489 |
496 | Alternatively, you can escape quotes by preceding them by a backslash (\). 497 | 498 |
500 | It is always a good idea to define all of your functions in the HEAD of your HTML page. 501 | This way, all functions will be defined before any content is displayed. 502 | Otherwise, the user might perform some action while the page is still loading that triggers 503 | an event handler and calls an undefined function, leading to an error. 504 | 505 |
507 | An array is an ordered set of values that you reference through an array name and an index. 508 | For example, you could have an array called emp, that contains employees' names 509 | indexed by their employee number. So emp[1] would be employee number one, emp[2] 510 | employee number two, and so on. 511 | 512 |
513 | JavaScript does not have an explicit array data type, but because of the intimate relationship 514 | between arrays and object properties (see JavaScript Object Model), 515 | it is easy to create arrays in JavaScript. 516 | You can define an array object type, as follows: 517 |
518 | function MakeArray(n) { 519 | this.length = n; 520 | for (var i = 1; i <= n; i++) { 521 | this[i] = 0 } 522 | return this 523 | } 524 | } 525 |526 |
527 | This defines an array such that the first property, length, (with index of zero), 528 | represents the number of elements in the array. 529 | The remaining properties have an integer index of one or greater, and are initialized to zero. 530 |
531 | You can then create an array by a call to new with the array name, 532 | specifying the number of elements it has. For example: 533 |
534 | emp = new MakeArray(20); 535 |536 |
537 | This creates an array called emp with 20 elements, and initializes the elements to zero. 538 | 539 |
541 | You can populate an array by simply assigning values to its elements. For example: 542 |
543 | emp[1] = "Casey Jones" 544 | emp[2] = "Phil Lesh" 545 | emp[3] = "August West" 546 |547 | and so on. 548 | 549 |
550 | You can also create arrays of objects. For example, suppose you define an object type 551 | named Employees, as follows: 552 | 553 |
554 | function Employee(empno, name, dept) { 555 | this.empno = empno; 556 | this.name = name; 557 | this.dept = dept; 558 | } 559 |560 |
561 | Then the following statements define an array of these objects: 562 | 563 |
564 | emp = new MakeArray(3) 565 | emp[1] = new Employee(1, "Casey Jones", "Engineering") 566 | emp[2] = new Employee(2, "Phil Lesh", "Music") 567 | emp[3] = new Employee(3, "August West", "Admin") 568 |569 |
570 | Then you can easily display the objects in this array using the show_props function 571 | (defined in the section on the JavaScript Object Model) as follows: 572 |
573 | for (var n =1; n <= 3; n++) { 574 | document.write(show_props(emp[n], "emp") + "577 |
"); 575 | } 576 |
578 | 579 | 580 | -------------------------------------------------------------------------------- /js-1.0/stmts.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 | Statements
6 |
7 |
JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semi-colon. 8 | 9 |
Syntax conventions: All keywords in syntax statements are in bold. Words in italics represent user-defined names or statements. Any portions enclosed in square brackets, [ ], are optional. {statements} indicates a block of statements, which can consist of a single statement or multiple statements delimited by a curly braces {}. 10 | 11 |
The following statements are available in JavaScript: 12 |
15 | |
23 |
24 | |
31 |
NOTE: new and this are not really statements, but are included in this section for convenience. 35 | 36 | 37 | 38 |
A statement that terminates the current while or for loop and transfers program control to the statement following the terminated loop. 41 | 42 | 43 |
45 | break 46 |47 | 48 | 49 |
The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x. 51 |
Notations by the author to explain what a script does. Comments are ignored by the interpreter. JavaScript supports Java-style comments: 68 |
76 | 1. // comment text 77 | 2. /* multiple line comment text */ 78 |79 | 80 | 81 |
A statement that terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. In contrast to the break statement, continue does not terminate the execution of the loop entirely: instead, 93 |
101 | continue102 | 103 | 104 | 105 |
The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12. 107 |
A statement that creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. 123 | 124 | 125 |
127 | for ([initial-expression;] [condition;] [increment-expression]) { 128 | statements 129 | } 130 |131 | 132 | initial-expression is a statement or variable declaration. It is typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. 133 |
condition is evaluated on each pass through the loop. If this condition evaluates to true, the statements in statements are performed. This conditional test is optional. If omitted, the condition always evaluates to true. 134 |
increment-expression is generally used to update or increment the counter variable. 135 |
statements is a block of statements that are executed as long as condition evaluates to true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements from the beginning of the for statement. 136 | 137 | 138 |
The following for statement starts by declaring the variable i and initializing it to zero. It checks that i is less than nine, performs the two succeeding statements, and increments i by one after each pass through the loop. 140 |
A statement that iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. 152 | 153 | 154 |
156 | for (variable in object) { 157 | statements } 158 |159 | 160 | variable is the variable to iterate over every property. 161 |
object is the object for which the properties are iterated. 162 |
statements specifies the statements to execute for each property. 163 | 164 |
The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values. 166 |
A statement that declares a JavaScript function name with the specified parameters param. Acceptable parameters include strings, numbers, and objects. 182 | 183 |
To return a value, the function must have a return statement that specifies the value to return. You cannot nest a function statement in another statement or in itself. 184 | 185 |
All parameters are passed to functions, by value. In other words, the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. 186 | 187 |
189 | function name([param] [, param] [..., param]) { 190 | statements } 191 |192 | 193 | 194 |
A statement that executes a set of statements if a specified condition is true. If the condition is false, another set of statements can be executed. 208 | 209 |
211 | if (condition) { 212 | statements1 } 213 | [else { 214 | statements2}] 215 |216 | 217 |
condition can be any JavaScript expression that evaluates to true or false. Parentheses are required around the condition. If condition evaluates to true, the statements in statements1 are executed. 218 |
statements1 and statements2 can be any JavaScript statements, including further nested if statements. Multiple statements must be enclosed in braces. 219 | 220 |
An operator that lets you create an instance of a user-defined object type. 234 | 235 |
Creating an object type requires two steps: 236 |
To define an object type, create a function for the object type that specifies its name, properties, and methods. An object can have a property that is itself another object. See the examples below. 241 | 242 |
You can always add a property to a previously defined object. For example, the statement car1.color = "black" adds a property color to car1, and assigns it a value of "black". However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of the car object type. 243 | 244 | 245 |
247 | objectName = new objectType ( param1 [,param2] ...[,paramN] ) 248 |249 | 250 | objectName is the name of the new object instance. 251 |
Example 1: object type and object instance.Suppose you want to create an object type for cars. You want this type of object to be called car, and you want it to have properties for make, model, year, and color. To do this, you would write the following function: 257 |
Now you can create an object called mycar as follows: 266 |
This statement creates mycar and assigns it the specified values for its properties. Then the value of mycar.make is the string "Eagle", mycar.year is the integer 1993, and so on. 271 | 272 |
You can create any number of car objects by calls to new. For example, 273 |
Example 2: object property that is itself another object. Suppose you define an object called person as follows: 278 |
286 | And then instantiate two new person objects as follows: 287 |
292 | Then you can rewrite the definition of car to include an owner property that takes a person object, as follows: 293 |
To instantiate the new objects, you then use the following: 303 |
Instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects rand and ken as the parameters for the owners. To find out the name of the owner of car2, you can access the following property: 309 |
A statement that specifies the value to be returned by a function. 318 | 319 | 320 |
322 | return expression 323 |324 | 325 | 326 |
The following function returns the square of its argument, x, where x is a number. 328 |
A keyword that you can use to refer to the current object. In general, in a method this refers to the calling object. 339 | 340 | 341 |
343 | this[.propertyName] 344 |345 | 346 | 347 |
Suppose a function called validate validates an object's value property, given the object and the high and low values: 349 |
You could call validate in each form element's onChange event handler, using this to pass it the form element, as in the following example: 356 |
A statement that declares a variable, optionally initializing it to a value. The scope of a variable is the current function or, for variables declared outside a function, the current application. 367 | 368 |
Using var outside a function is optional; you can declare a variable by simply assigning it a value. However, it is good style to use var, and it is necessary in functions if a global variable of the same name exists. 369 | 370 | 371 |
373 | var varname [= value] [..., varname [= value] ] 374 |375 | 376 | varname is the variable name. It can be any legal identifier. 377 |
A statement that creates a loop that evaluates an expression, and if it is true, executes a block of statements. The loop then repeats, as long as the specified condition is true. 389 | 390 | 391 |
393 | while (condition) { 394 | statements 395 | } 396 |397 | 398 | condition is evaluated before each pass through the loop. If this condition evaluates to true, the statements in the succeeding block are performed. When condition evaluates to false, execution continues with the statement following statements. 399 |
statements is a block of statements that are executed as long as the condition evaluates to true. Although not required, it is good practice to indent these statements from the beginning of the while statement. 400 | 401 |
The following while loop iterates as long as n is less than three. 403 | 404 |
Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values: 414 |
After completing the third pass, the condition n < 3 is no longer true, so the loop terminates. 421 | 422 | 423 | 424 |
A statement that establishes a the default object for a set of statements. Within the set of statements, any property references that do not specify an object are assumed to be for the default object. 427 | 428 | 429 |
431 | with (object){ 432 | statements 433 | } 434 |435 | 436 | object specifies the default object to use for the statements. The parentheses are required around object. 437 |
The following with statement specifies that the Math object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references. 441 |
JavaScript supports a compact set of statements that you can use to incorporate a great deal of interactivity in web pages. The statements are: 6 |
The following sections provide a brief overview of each statement. See the statements reference for detailed information about statement syntax. 33 | 34 | 35 |
Conditional statements let you perform certain actions based on a logical condition. You specify a condition to test and the commands to execute if the condition is true. JavaScript has one conditional statement: the if statement. 38 | 39 |
An if statement is an either/or switch. If a specified condition is true, JavaScript performs certain statements. If the condition is false, JavaScript might perform other statements. An if statement looks as follows: 41 | 42 |
43 | if (condition) { 44 | statements1 } 45 | [else { 46 | statements2}] 47 |48 | 49 |
The condition can be any JavaScript expression that evaluates to true or false. The conditional statements can be any JavaScript statements, including further nested if statements. Multiple statements must be enclosed in braces. 50 | 51 |
Example. In the following example, the function checkData() returns true if the number of characters in a text object is three; otherwise, it displays an alert and returns false. 52 |
A loop is a set of commands that executes repeatedly until a specified condition is met. JavaScript supports two loop structures: for and while. In addition, the break and continue statements are used specifically with loops. 66 | 67 |
Another statement, for...in, executes statements repeatedly but is used for object manipulation. See Object Manipulation Statements. 68 | 69 |
A for loop repeats a loop until a specified condition evaluates to false. The JavaScript for loop is similar to the Java for loop and the traditional for loop in C. A for statement looks as follows: 71 | 72 |
73 | for ([initial-expression;] [condition;] [increment-expression]) { 74 | statements 75 | } 76 |77 | 78 |
When a for loop is encountered, the initial-expression is executed. The statements are executed as long as condition evaluates to true. The increment-expression is performed on each pass through the loop. The sequence of execution is as follows: 79 |
Example. The following function contains a for statement that counts the number of selected options in a scrolling list (a select object that allows multiple selections). The for statement declares the variable i and initializes it to zero. It checks that i is less than the number of options in the select object, performs the succeeding if statement, and increments i by one after each pass through the loop. 87 |
A while statement repeats a loop as long as a specified condition evaluates to true. A while statement looks as follows: 115 | 116 |
117 | while (condition) { 118 | statements 119 | } 120 |121 | 122 |
If the condition becomes false, the statements within the loop stop executing and control passes to the statement following the loop. 123 | 124 |
The condition test occurs only when the statements in the loop have been executed and the loop is about to be repeated. That is, the condition test is not continuous, but is performed once at the beginning of the loop and again just following the last statement in statements, each time control passes through the loop. 125 | 126 |
Example 1. The following while loop iterates as long as n is less than three. 127 | 128 |
Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values: 138 |
After completing the third pass, the condition n < 3 is no longer true, so the loop terminates. 145 | 146 |
Example 2: infinite loop. Make sure the condition in a loop eventually becomes false; otherwise, the loop will never terminate. The statements in the following while loop execute forever because the condition never becomes false: 147 | 148 |
The break statement terminates the current while or for loop and transfers program control to the statement following the terminated loop. A break statement looks as follows: 155 | 156 |
157 | break 158 |159 | 160 |
Example. The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x. 161 |
A continue statement terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. A continue statement looks as follows: 175 |
176 | continue177 | 178 | 179 |
In contrast to the break statement, continue does not terminate the execution of the loop entirely. Instead, 180 |
Example. The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12. 186 |
JavaScript has several ways of manipulating objects: for...in statement, new operator, this keyword, and with statement. 201 | 202 |
The for...in statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. A for...in statement looks as follows: 204 | 205 |
206 | for (variable in object) { 207 | statements } 208 |209 | 210 |
Example. The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values. 211 |
For an object car with properties make and model, result would be: 223 |
The new operator lets you create an instance of a user-defined object type. Use new as follows: 230 | 231 |
232 | objectName = new objectType ( param1 [,param2] ...[,paramN] ) 233 |234 | 235 |
For information, see new in the statements reference. 236 | 237 |
Use the this keyword to refer to the current object. In general, this refers to the calling object in a method. Use this as follows: 239 | 240 |
241 | this[.propertyName] 242 |243 | 244 |
Example. Suppose a function called validate validates an object's value property, given the object and the high and low values: 245 |
You could call validate in each form element's onChange event handler, using this to pass it the form element, as in the following example: 252 |
The with statement establishes the default object for a set of statements. Within the set of statements, any property references that do not specify an object are assumed to be for the default object. A with statement looks as follows: 260 | 261 |
262 | with (object){ 263 | statements 264 | } 265 |266 | 267 |
Example. The following with statement specifies that the Math object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references. 268 |
Comments are notations by the author to explain what a script does. Comments are ignored by the interpreter. JavaScript supports Java-style comments: 282 |
Example.. The following example shows two comments. 288 |
301 | 302 | -------------------------------------------------------------------------------- /js-1.0/toc.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |JavaScript lets you create and open windows for presenting HTML text, form objects, and frames. The window object is the top-level object in the JavaScript client hierarchy. Form elements and all JavaScript code exists in documents that are loaded into windows. By understanding how windows work, you can control and manipulate these windows. 5 | 6 |
A window is created automatically when a user launches Navigator, and a user can open a window by choosing New Web Browser from the Navigator's File menu. A user can close a window by choosing either Close or Exit from the Navigator's File menu. You can also open and close windows programmatically.
18 |
19 | Opening a window
20 |
You can create a window with the open method. The following statement creates a window called msgWindow that displays the contents of the file sesame.html: 21 |
The following statement creates a window called homeWindow that displays the Netscape home page: 26 |
Windows can have two names. The following statement creates a window with two names. The first name, "msgWindow", is used when referring to the window's properties, methods, and containership; the second name, "displayWindow", is used when referring to the window as the target of a form submit or hypertext link. 31 | 32 |
When you create a window, a window name is not required. But if you want to refer to a window from another window, the window must have a name. For more information on using window names, see Referring to windows and frames. 37 | 38 |
When you open a window, you can specify attributes such as the window's height and width and whether the window contains a toolbar, location field, or scrollbars. The following statement creates a window without a toolbar but with scrollbars: 39 |
For details on these window attributes, see the open method.
45 |
46 | Closing a window
47 |
You can close a window programmatically with the close method. You cannot close a frame without closing the entire parent window. 48 | 49 |
All of the following statements close the current window: 50 |
The following statement closes a window called msgWindow: 58 |
A frame is a special type of window that can display multiple, independently scrollable frames on a single screen, each with its own distinct URL. Frames can point to different URLs and be targeted by other URLs, all within the same window. A series of frames makes up a page. 67 | 68 |
The following diagram shows a window containing three frames: 69 |
You create a frame by using the <FRAMESET> tag in an HTML document. The <FRAMESET> tag is used in an HTML document whose sole purpose is to define the layout of frames that make up a page. 100 | 101 |
Example 1. The following statement creates the frameset shown in the previous diagram. 102 |
The following diagram shows the hierarchy of the frames. All three frames have the same parent, even though two of the frames are defined within a separate frameset. This is because a frame's parent is its parent window, and a frame, not a frameset, defines a window. 113 |
You can refer to the previous frames using the frames array as follows. (For information on the frames array, see the frame object.) 124 |
Example 2. Alternatively, you could create a window like the previous one but in which the top two frames have a separate parent from navigateFrame. The top-level frameset would be defined as follows: 131 | 132 |
The file muskel3.html contains the skeleton for the upper frames and defines the following frameset: 140 |
The following diagram shows the hierarchy of the frames. upperFrame and navigateFrame share a parent: the top window. listFrame and contentFrame share a parent: upperFrame. 148 |
You can refer to the previous frames using the frames array as follows. (For information on the frames array, see the frame object.) 163 |
You can update the contents of a frame by using the location property to set the URL, as long as you specify the frame hierarchy. 173 | 174 |
For example, suppose you are using the frameset described in Example 2 in the previous section. If you want users to be able to close the frame containing the alphabetic or categorical list of artists (in the frame listframe) and view only the music titles sorted by musician (currently in the frame contentFrame), you could add the following button to navigateFrame. 175 | 176 |
If you want users to be able to close the frame containing the alphabetic or categorical list of artists (in the frame listframe) and view only the music titles sorted by musician (currently in the frame contentFrame), you could add the following button to navigateFrame. 177 |
When a user clicks this button, the file artists.html is loaded into the frame upperFrame; the frames listFrame and contentFrame close and no longer exist. 183 | 184 |
Because a frame is a type of window, you refer to frames and navigate among frames the same as you do with a window. See Referring to windows and frames and Navigating among windows. 186 | 187 | 188 |
If the frameset in the previous section is designed to present the available titles for a music club, the frames and their HTML files could have the following content: 190 |
The file category.html (the categorical list) contains code similar to the following: 198 |
Jazz 201 |
Soul 207 |
The file alphabet.html (the alphabetical list) contains code similar to the following: 213 |
The file navigate.html (the navigational links at the bottom of the screen) contains code similar to the following. Notice that the target for artists.html is "_parent". When the user clicks this link, the entire window is overwritten, because the top window is the parent of navigateFrame. 223 |
The file titles.html (the main file, displayed in the frame on the right) contains code similar to the following: 233 |
Interlude
237 |
238 | The Beatles
239 |
Please Please Me
240 |
241 | Betty Carter
242 |
Ray Charles and Betty Carter 243 | ... 244 |
For details on the syntax for creating a frame, see the frame object. 247 | 248 | 249 | 250 |
The name you use to refer to a window depends on whether you are referring to a window's properties, methods, and event handlers or are referring to the window as the target of a form submit or hypertext link. 253 | 254 |
Since the window object is the top-level object in the JavaScript client hierarchy, the window is essential for specifying the containership of objects in any window. 255 | 256 | 257 |
You can refer to the properties, methods, and event handlers of the current window or another window (if the other window is named) in any of the following ways: 259 | 260 |
For more information on these methods of referring to windows, see the window object. 271 | 272 |
Example 1: refer to the current window. The following statement refers to a form named musicForm in the current window. The statement displays an alert if a checkbox is checked. 273 |
Example 2: refer to another window. The following statements refer to a form named musicForm in a window named checkboxWin. The statements determine if a checkbox is checked, check the checkbox, determine if the second option of a select object is selected, and select the second option of the select object. Even though object values are changed in checkboxWin, the current window remains active: checking the checkbox and selecting the selection option do not give focus to the window. 279 |
Example 3: refer to a frame in another window. The following statement refers to a frame named frame2 that is in a window named window2. The statement changes the background color of frame2 to violet. The frame name, frame2, must be specified in the <FRAMESET> tag that creates the frameset. 296 |
Use a window's name (not the window variable) when referring to a window as the target of a form submit or hypertext link (the TARGET attribute of a <FORM> or <A> tag). The window you specify is the window that the link is loaded into or, for a form, the window that server responses are displayed in. 303 | 304 |
Example 1: second window. The following example creates a hypertext link to a second window. The example has a button that opens a window named window2, then a link that loads the file doc2.html into the newly opened window, then a button that closes the window. 305 |
307 | 309 |
310 | Load a file into window2 311 |
312 | 314 |
Example 2: anchor in a second window. The following example creates a hypertext link to an anchor in a second window. The link displays the anchor named numbers in the file doc2.html in the window window2. If window2 does not exist, it is created. 317 |
Example 3: frame name. The following example creates a hypertext link to an anchor in a frame. The link displays the anchor named abs_method in the file sesame.html in the frame named "contentFrame". The frame must be within the current frameset and the frame name must be defined in the NAME attribute of a <FRAME> tag. 322 |
Example 4: literal frame name. The following example creates a hypertext link to a file. The link displays the file named artists.html in the parent window of the current frameset. This link object appears in a frame within a frameset, and when the user clicks the link, all frames in the frameset disappear and the content of artists.html is loaded into the parent window. 327 |
Many Navigator windows can be open at the same time. The user can move among these windows by clicking them to give them focus. You can give focus to a window programmatically by giving focus to an object in the window or by specifying the window as the target of a hypertext link. Although you can change an object's values in a second window, that does not make the second window active: the current window remains active. 337 | 338 |
The active window is the window that has focus. When a window has focus, it is brought to the front and changes in some visual way. For example, the window's title bar might change to a different color. The visual cue varies depending on the platform you are using. 339 | 340 |
Example 1: give focus to an object in another window. The following statement gives focus to a text object named city in a window named checkboxWin. Because the text object is gaining focus, checkboxWin also gains focus and becomes the active window. The example also shows the statement that creates checkboxWin. 341 |
Example 2: give focus to another window using a hypertext link. The following statement specifies window2 as the target of a hypertext link. When the user clicks the link, focus switches to window2. If window2 does not exist, it is created. 348 |