-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2110 lines (1888 loc) · 100 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="DCTERMS.language" scheme="RFC1766" content="en">
<meta name="DC.title" content="comp.lang.javascript Frequently Asked Questions">
<meta name="DCTERMS.subject" content="Frequently asked questions in the Usenet newsgroup comp.lang.javascript">
<meta name="DC.format" content="text/html">
<meta name="DC.type" content="Text">
<meta name="DC.creator" content="Jim Ley">
<meta name="DC.publisher" content="Garrett Smith">
<META name="DC.Publisher.Address" content="dhtmlkitchen@gmail.com">
<meta name="DCTERMS.modified" content="2010-10-08">
<meta name="DCTERMS.audience" content="Programmers, web developers">
<meta name="DC.description" content="Javascript Frequently Asked Questions">
<meta name="DC.identifier" content="http://jibbering.com/faq">
<meta name="DC.source" content="http://www.ecma-international.org/publications/standards/Ecma-262.htm">
<meta name="DC.source" content="news:comp.lang.javascript">
<meta name="DC.source" content="https://developer.mozilla.org/en/JavaScript">
<meta name="DC.source" content="http://msdn.microsoft.com/en-us/library/hbxc2t98%28VS.85%29.aspx">
<meta name="DC.source" content="http://msdn.microsoft.com/en-us/library/ms533050%28VS.85%29.aspx">
<meta name="DC.rights" content="copyright contributors, comp.lang.javascript">
<link rel="StyleSheet" href="faq.css" type="text/css" media="screen">
<title> comp.lang.javascript FAQ</title>
</head>
<body>
<h1> comp.lang.javascript FAQ</h1>
<p>Version 32.2, Updated 2010-10-08, by Garrett Smith</p><div id="nav"><a href="notes/">FAQ Notes</a></div>
<ul id='faqList'>
<li>1 <a href='#meta'>Meta-FAQ meta-questions</a>
<ul>
<li>1.1 <a href='#newsgroups'>Which newsgroups deal with javascript?</a></li>
<li>1.2 <a href='#appropriateQuestions'>What questions are on-topic for comp.lang.javascript?</a></li>
<li>1.3 <a href='#posting'>What should I do before posting to comp.lang.javascript?</a></li>
<li>1.4 <a href='#noAnswer'>Why was my post not answered?</a></li>
</ul></li><li>2 <a href='#tips'>Language Overview</a>
<ul>
<li>2.1 <a href='#ecma'>What is ECMAScript?</a></li>
<li>2.2 <a href='#jScript'>What is JScript?</a></li>
<li>2.3 <a href='#dom'>What is the Document Object Model (DOM)?</a></li>
<li>2.4 <a href='#localization'>Internationalisation and Localisation in javascript</a></li>
<li>2.5 <a href='#futureEcmaScript'>What does the future hold for ECMAScript?</a></li>
</ul></li><li>3 <a href='#ecmascriptResources'>Javascript Resources</a>
<ul>
<li>3.1 <a href='#books'>What books are recommended for javascript?</a></li>
<li>3.2 <a href='#onlineResources'>What online resources are available?</a></li>
<li>3.3 <a href='#libraryResources'>Javascript Libraries</a></li>
</ul></li><li>4 <a href='#functions'>Functions</a>
<ul>
<li>4.1 <a href='#scope'>What is (function(){ /*...*/ })() ?</a></li>
<li>4.2 <a href='#functionStatement'>What is a function statement?</a></li>
</ul></li><li>5 <a href='#dates'>Dates</a>
<ul>
<li>5.1 <a href='#formatDate'>How do I format a Date object with javascript?</a></li>
<li>5.2 <a href='#parseDate'>How can I create a Date object from a String?</a></li>
</ul></li><li>6 <a href='#numbers'>Numbers</a>
<ul>
<li>6.1 <a href='#formatNumber'>How do I format a Number as a String with exactly 2 decimal places?</a></li>
<li>6.2 <a href='#binaryNumbers'>Why does simple decimal arithmetic give strange results?</a></li>
<li>6.3 <a href='#parseIntBase'>Why does K = parseInt('09') set K to 0?</a></li>
<li>6.4 <a href='#typeConvert'>Why does 1+1 equal 11? or How do I convert a string to a number?</a></li>
<li>6.5 <a href='#randomNumber'>How do I generate a random integer from 1 to n?</a></li>
</ul></li><li>7 <a href='#objects'>Objects</a>
<ul>
<li>7.1 <a href='#nativeObject'>What is a native object?</a></li>
<li>7.2 <a href='#builtInObject'>What is a built-in object?</a></li>
<li>7.3 <a href='#hostObject'>What is a host object?</a></li>
<li>7.4 <a href='#eval'>When should I use eval?</a></li>
<li>7.5 <a href='#propertyAccessAgain'>How do I access a property of an object using a string?</a></li>
</ul></li><li>8 <a href='#strings'>Strings and RegExp</a>
<ul>
<li>8.1 <a href='#trimString'>How do I trim whitespace?</a></li>
</ul></li><li>9 <a href='#domRef'>DOM and Forms</a>
<ul>
<li>9.1 <a href='#formControlAccess'>How do I get the value of a form control?</a></li>
<li>9.2 <a href='#propertyAccess'>My element is named myselect[], how do I access it?</a></li>
<li>9.3 <a href='#globalPollution'>Why doesn't the global variable "divId" always refer to the element with id="divId"?</a></li>
<li>9.4 <a href='#updateContent'>How do I modify the content of the current page?</a></li>
<li>9.5 <a href='#accessElementBeforeDefined'>Why does my code fail to access an element?</a></li>
<li>9.6 <a href='#testCookie'>How can I see in javascript if a web browser accepts cookies?</a></li>
</ul></li><li>10 <a href='#windows'>Windows and Frames</a>
<ul>
<li>10.1 <a href='#disableBackButton'>How can I disable the back button in a web browser?</a></li>
<li>10.2 <a href='#frameRef'>How do I access a frame's content?</a></li>
<li>10.3 <a href='#getWindowSize'>How do I find the size of the window?</a></li>
<li>10.4 <a href='#isWindowOpen'>How do I check to see if a child window is open, before opening another?</a></li>
<li>10.5 <a href='#printFrame'>Why does framename.print() not print the correct frame in IE?</a></li>
<li>10.6 <a href='#windowClose'>How do I close a window and why does it not work on the first one?</a></li>
<li>10.7 <a href='#permissionDenied'>Why do I get permission denied when accessing a frame/window?</a></li>
<li>10.8 <a href='#setTimeout'>How do I make a 10 second delay?</a></li>
<li>10.9 <a href='#printSettings'>How do I change print settings for window.print()?</a></li>
<li>10.10 <a href='#changeBrowserDialog'>How do I change the confirm box to say yes/no or default to cancel?</a></li>
<li>10.11 <a href='#fileDownload'>How do I prompt a "Save As" dialog for an accepted mime type?</a></li>
<li>10.12 <a href='#modifyChrome'>How do I modify the current browser window?</a></li>
<li>10.13 <a href='#target'>How do I POST a form to a new window?</a></li>
<li>10.14 <a href='#openWindow'>How do I open a new window with javascript?</a></li>
</ul></li><li>11 <a href='#ajaxRef'>Ajax and Server Communication</a>
<ul>
<li>11.1 <a href='#ajax'>What is Ajax?</a></li>
<li>11.2 <a href='#downloadPage'>How do I download a page to a variable?</a></li>
<li>11.3 <a href='#getServerVariable'>How do I get a jsp/php variable into client-side javascript?</a></li>
<li>11.4 <a href='#sessionExpired'>How do I log-out a user when they leave my site?</a></li>
<li>11.5 <a href='#runServerScript'>How do I run a server side script?</a></li>
<li>11.6 <a href='#noCache'>How do I force a reload from the server/prevent caching?</a></li>
<li>11.7 <a href='#ajaxCache'>Why is my Ajax page not updated properly when using an HTTP GET request in Internet Explorer?</a></li>
</ul></li><li>12 <a href='#debugging'>Debugging</a>
<ul>
<li>12.1 <a href='#javascriptErrors'>How do I get my browser to report javascript errors?</a></li>
</ul></li><li>13 <a href='#doNotTry'>Things not to attempt in a browser</a>
<ul>
<li>13.1 <a href='#detectBrowser'>How do I detect Opera/Safari/IE?</a></li>
<li>13.2 <a href='#preventAccess'>How can I prevent access to a web page by using javascript?</a></li>
<li>13.3 <a href='#hideSource'>How do I protect my javascript code?</a></li>
<li>13.4 <a href='#disableRightClick'>How do I suppress a context menu (right-click menu)?</a></li>
<li>13.5 <a href='#readFile'>How can I access the client-side filesystem?</a></li>
<li>13.6 <a href='#javascriptURI'>I have <a href="javascript:somefunction()"> what ... ?</a></li>
</ul></li><li>14 <a href='#comments'>Comments and Suggestions</a>
<ul>
<li>14.1 <a href='#FAQENTRY'>Why do some posts have <FAQENTRY> in them?</a></li>
<li>14.2 <a href='#makeSuggestion'>How do I make a suggestion?</a></li>
</ul></li></ul><div id='meta' class='section'><h2 id='FAQ1'>1 Meta-FAQ meta-questions</h2>
<p>
This is the <em>comp.lang.javascript</em> meta-FAQ, 32.2. The latest
version is available at <a href="http://jibbering.com/faq/" >http://jibbering.com/faq/</a> in HTML form.
</p> <p>
Each day, one section of the FAQ is posted for review and questions,
and as a reminder that the FAQ is available.
</p> <p>
For additional explanation and detail relating to some aspects
of the FAQ, please see the
<a href="notes/" >FAQ Notes</a>.
It has been provided separately to avoid increasing the size of
the FAQ to a point where it would be unreasonable to post it to
the group.
</p> <p>
Code examples in this FAQ use <a href="http://jsdoctoolkit.org/" >JSDoc Toolkit</a> comments.
</p> </div><div id='newsgroups' class='section'><h3 id='FAQ2_1'>1.1 Which newsgroups deal with javascript?</h3><p>
The official Big 8 Usenet newsgroup dealing with javascript is
<a href="news:comp.lang.javascript">comp.lang.javascript</a>.
Some "language" hierarchies also have *.comp.lang.javascript groups.
</p> <p>
c.l.js is an unmoderated newsgroup.
</p> </div>
<div id='appropriateQuestions' class='section'><h3 id='FAQ2_2'>1.2 What questions are on-topic for comp.lang.javascript?</h3><p>
The comp.lang.javascript newsgroup deals with ECMAScript
languages, so any questions about JavaScript or JScript are
welcome. However, the majority of questions sent to this group
relates to javascript in a web browser. If you are experiencing
issues with a particular browser, or the host is not a browser
at all, please make this information clear.
</p> <p>
Javascript and Java are two completely different languages.
Java questions should be asked in one of the comp.lang.java.*
newsgroups; they are not appropriate for c.l.js (as Java and
javascript are distinct programming languages with only
superficial similarities due to sharing a C-like syntax and
some of the characters in their names).
</p> <p>
Questions dealing with other scripting languages, such as
VBScript, PerlScript or CGI scripting are also off-topic,
as are HTML-only or CSS-only questions.
</p> <p>
Questions that are specific to Microsoft's JScript may also
be appropriately asked at:
<a href="news:microsoft.public.scripting.jscript">microsoft.public.scripting.jscript</a></p> <p>
The comp.lang.javascript newsgroup charter is included in
<a href="faq_notes/cljs_charter.html" >faq_notes/cljs_charter.html</a>.
</p> </div>
<div id='posting' class='section'><h3 id='FAQ2_3'>1.3 What should I do before posting to comp.lang.javascript?</h3><p>
Before posting to c.l.js, you should read this document.
You should also check the <a href="#onlineResources" >Resources section</a>.
</p>
<h4 id='ask'>How to Ask a Question</h4>
<ul>
<li>
State your question clearly and concisely.
</li>
<li>
Use the Subject: line to show the type of problem you have but
include the question in the body as well.
</li>
<li>
For a more detailed explanation of formatting, see
<a href="notes/posting/" >"Posting Questions and Replies to comp.lang.javascript"</a>.
</li></ul>
<h4 id='reply'>Replying</h4>
<ul>
<li>
Quote only relevant parts of earlier messages, and add your
comments below each quoted section
(<a href="http://www.ietf.org/rfc/rfc1855.txt" >FYI28/RFC1855</a>).
</li>
<li>
Link to specific sections of the FAQ that are relevant.
</li>
<li>
Avoid being unnecessarily rude, but do not complain about other rude posts.
</li>
<li>
Don't quote signatures.
</li></ul>
<h4 id='postCode'>Posting Code</h4>
<ul>
<li>
Remove everything that does not contribute to the problem (images,
markup, other scripts, etc).
</li>
<li>
Validate the HTML and CSS <a href="http://validator.w3.org/" >http://validator.w3.org/</a>, <a href="http://jigsaw.w3.org/css-validator/" >http://jigsaw.w3.org/css-validator/</a>.
</li>
<li>
Make sure the code is executable as transmitted.
</li>
<li>
Format lines to 72 characters; indent with 2-4 spaces (not tabs).
</li>
<li>
State what you expect the code to do.
</li>
<li>
Mention the platforms, browsers, and versions.
</li>
<li>
See also the <a href="#debugging" >FAQ section on debugging</a>.
</li>
<li>
Post in plain-text only. Do not encode it. Do not attach files.
</li>
<li>
If the code is more than about 100 lines, provide a URL in addition.
</li>
<li>
Do not multi-post; cross-post if necessary
(<a href="http://en.wikipedia.org/wiki/Cross-post" >Wikipedia description</a>).
</li></ul>
<h4 id='doNotPost'>What Not to Post</h4>
<ul>
<li>
Do not post job postings. Job postings should go to
an appropriate regional jobs group.
</li>
<li>
Do not post copyright material without permission
from the copyright holder.
</li></ul><p>
Relevant announcements are welcome, but no more often than once
per major release, as a short link to the product's webpage.
</p> </div>
<div id='noAnswer' class='section'><h3 id='FAQ2_4'>1.4 Why was my post not answered?</h3><p>
This could be for several reasons:
</p>
<ul> <li>
It was a variation of a frequently asked question and was
therefore ignored by everyone.
</li>
<li>
Nobody knows the answer.
</li>
<li>
The person with the answer has not seen the post.
</li>
<li>
It might not be possible to do what you want to do but perhaps
readers of c.l.js are reluctant to answer your post in the negative
when they are not convinced that it cannot be done.
</li>
<li>
The question was not asked clearly enough, or did not included
enough information to be answered.
</li>
<li>
The questioner did not realise the need to read the group, for a
few days, to see the answers posted there.
</li>
<li> You ignored the <a href="#posting" >section on posting</a> </li>
</ul>
<p>
If it is not one of these, then after a few days consider
reposting after checking <a href="http://groups.google.com/group/comp.lang.javascript/topics" >http://groups.google.com/group/comp.lang.javascript/topics</a>
for replies. Make sure the post is phrased well, and everything
needed to answer is correct, and the subject is appropriate.
</p> </div>
<div id='tips' class='section'><h2 id='FAQ2'>2 Language Overview</h2>
</div><div id='ecma' class='section'><h3 id='FAQ2_6'>2.1 What is ECMAScript?</h3><p>
<a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" >ECMA-262</a>
is the international standard that current language implementations
(JavaScript™, JScript etc.) are based on.
</p> <p>
<a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" >ECMA-262</a>
defines the language Syntax, Types, Keywords, Operators, and built-in
objects. The ECMAScript specification is the reference to determine the
expected behavior of a program. ECMAScript does not define any host
objects, such as <code>document</code>, <code>window</code>, or <code>ActiveXObject</code>.
</p> <p>
ECMA-327 defines the Compact Profile of ECMAScript by
describing the features from ECMA 262 that may be omitted in some
resource-constrained environments.
<a href="http://www.ecma-international.org/publications/standards/Ecma-327.htm" >http://www.ecma-international.org/publications/standards/Ecma-327.htm</a></p> <p>
The most widely supported edition of ECMA-262 is the 3rd edition (1999).
There is fair support for this edition in JScript 5.5+ (buggy) and good
support JavaScript 1.5.
</p> <p>
The term "javascript" is used as a common name for all dialects of ECMAScript.
</p> </div>
<div id='jScript' class='section'><h3 id='FAQ2_7'>2.2 What is JScript?</h3><p>
JScript is Microsoft's implementation of ECMAScript.
</p> <p>
Questions that are specific to Microsoft's JScript may also
be appropriately asked at:
<a href="news:microsoft.public.scripting.jscript">microsoft.public.scripting.jscript</a>.
</p> </div>
<div id='dom' class='section'><h3 id='FAQ2_9'>2.3 What is the Document Object Model (DOM)?</h3><p>
The
<dfn>Document Object Model</dfn> (DOM) is a interface-based model for <code>Document</code>
objects. The DOM allows scripts to dynamically access and update a
document's content, style, and event handlers.
</p> <p>
The DOM is <em>not</em> part of the ECMAScript programming language.
</p> <p>
Official DOM standards are defined by the World Wide Web Consortium.
Scriptable browsers also have
<dfn>proprietary</dfn> DOM features (<a href="http://msdn.microsoft.com/en-us/library/ms533050(VS.85).aspx" >MSDN</a>, <a href="https://developer.mozilla.org/en/DOM_Client_Object_Cross-Reference" >MDC</a>),
such as <code>document.writeln()</code>.
</p> <p>
Also see the section on <a href="#domRef" >DOM and Forms</a>.
</p><ul class='linkList'><li><a href="#onlineResources" >c.l.js DOM Resources</a></li>
<li><a href="http://www.w3.org/DOM/faq.html" >W3C DOM FAQ</a></li>
<li><a href="http://www.w3.org/DOM/" >W3C DOM </a></li>
<li><a href="https://developer.mozilla.org/en/Gecko_DOM_Reference/Introduction#What_is_the_DOM.3F" >MDC: What is the DOM?</a></li>
</ul> </div>
<div id='localization' class='section'><h3 id='FAQ2_10'>2.4 Internationalisation and Localisation in javascript</h3><p>
<dfn>Internationalisation</dfn> means using one form which is everywhere both
acceptable and understood. Any international standard not supported by
default can be coded for.
</p> <p>
For example, there is an International Standard for numeric Gregorian
date format; but none for decimal and thousands separators.
</p> <p>
<dfn>Localisation</dfn> is the process of adapting software for a specific region
or language by adding locale-specific components and translating text. It
cannot work well in general, because it requires a knowledge of all
preferences and the ability to choose the right one, in an environment
where many systems are inappropriately set anyway.
</p> <p>
ECMAScript has a few
<dfn>localisation</dfn> features. The various
<code>toString()</code> methods are all implementation dependent,
but tend to use either UK or US settings (not necessarily correctly).
ECMAScript Ed. 3 introduced some capabilities, including the
<code>toLocaleString()</code>method which should create a string
based on the host's locale.
</p> <p>
ECMAScript 5th Edition introduces limited ISO 8601 capabilities with
<code>Date.prototype.toISOString()</code> and new behavior for <code>Date.parse()</code>.
</p> </div>
<div id='futureEcmaScript' class='section'><h3 id='FAQ2_12'>2.5 What does the future hold for ECMAScript?</h3><p>
The 5th edition of ECMAScript was approved on 2009-12-04. There is some
support in implementations released before approval date (JScript 5.8,
JavaScript 1.8, JavaScriptCore).
http://www.ecma-international.org/publications/standards/Ecma-262.htm
</p><ul class='linkList'><li><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" >http://www.ecma-international.org/publications/standards/Ecma-262.htm</a></li>
</ul> </div>
<div id='ecmascriptResources' class='section'><h2 id='FAQ3'>3 Javascript Resources</h2>
</div><div id='books' class='section'><h3 id='FAQ3_1'>3.1 What books are recommended for javascript?</h3><p>
Most javascript books have been found to contain so many technical
errors that consensus recommendations have not emerged from the group.
</p> <p>
The following books have been considered to have value by some
individuals on c.l.js. The reviews of these books are provided:
</p>
<ul>
<li> <em>"JavaScript: The Definitive Guide,"</em> 5th Edition, by David Flanagan
<ul> <li> Published: 2006-08 </li>
<li> Pages: 1018 </li>
<li> Errata: <a href="http://oreilly.com/catalog/9780596101992/errata/" >http://oreilly.com/catalog/9780596101992/errata/</a> </li>
<li> Discussed in:
<ul> <li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/7283898f77fd2a66/9252aa024e058dea#c5f145ae807c918e" >FAQ Update 9.85 Dated 2007-08-31</a> </li>
</ul>
</li>
</ul>
</li>
<li> <em>"JavaScript, The Good Parts,"</em> 1st Edition, by Douglas Crockford
<ul> <li> Published: 2008-05 </li>
<li> Pages: 170 </li>
<li> Errata: <a href="http://oreilly.com/catalog/9780596517748/errata/" >http://oreilly.com/catalog/9780596517748/errata/</a> </li>
<li> Discussed in:
<ul> <li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/d084d2109f7b4ec7#" >Crockford's 'The Good Parts': a short review</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/db1e49ab113aa05c/3987eac87ad27966#3987eac87ad27966" >FunctionExpression's and memory consumptions</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/6a41f7835ee728de/da5ccfc65e2df64a#da5ccfc65e2df64a" >FAQ Topic - What books are recommended for javascript? (2008-12-02)</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/3a08fb741525ab6d/" >Augmenting functions</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/bf26be6e63494ee2/acb733a1c35f6ce5#ee9e4ee29e658d5d" >Crockford's JavaScript, The Good Parts (a book review).</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/df602506ee48b400/e65e00f5cad07676#e65e00f5cad07676" >Closures Explained</a> </li>
<li> <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/acadf1b22b219433/4f69a95607d0b3ae#4f69a95607d0b3ae" >Javascript library development</a> </li>
</ul>
</li>
</ul>
</li></ul></div>
<div id='onlineResources' class='section'><h3 id='FAQ3_2'>3.2 What online resources are available?</h3>
<h4 id='ecmaResources'>ECMAScript</h4>
<dl>
<dt>
The Official ECMAScript Specification
</dt>
<dd> <a href="[ECMA-262] http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm" >[ECMA-262] http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm</a> </dd>
<dt>
[ISO16262] ISO/IEC 16262, Second Edition 2002-06-01 : ISO Standard matching
ECMA-262 3rd Edition, with corrections.
</dt>
<dd> <a href="http://standards.iso.org/ittf/PubliclyAvailableStandards/c033835_ISO_IEC_16262_2002(E).zip" >http://standards.iso.org/ittf/PubliclyAvailableStandards/c033835_ISO_IEC_16262_2002(E).zip</a> </dd>
<dt>
[MS-ES3]: Internet Explorer ECMA-262 ECMAScript Language Specification Standards Support
</dt>
<dd> <a href="http://msdn.microsoft.com/en-us/library/ff520996%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/ff520996%28VS.85%29.aspx</a> </dd>
<dd> <a href="res/%5BMS-ES3%5D.pdf" >res/%5BMS-ES3%5D.pdf</a> (local alias) </dd>
<dt>
[MS-ES3EX]: Microsoft JScript Extensions to the ECMAScript Language Specification Third Edition
</dt>
<dd> <a href="http://msdn.microsoft.com/en-us/library/ff521046%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/ff521046%28VS.85%29.aspx</a> </dd>
<dd> <a href="res/%5BMS-ES3EX%5D.pdf" >res/%5BMS-ES3EX%5D.pdf</a> (local alias) </dd>
<dt> ECMAScript on Wikipedia
</dt>
<dd> <a href="http://en.wikipedia.org/wiki/ECMAScript" >http://en.wikipedia.org/wiki/ECMAScript</a> </dd></dl>
<h4 id='domResources'>W3C DOM</h4>
<dl>
<dt>
DOM Level 1 ECMAScript Binding
</dt>
<dd> <a href="http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html" >http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html</a> </dd>
<dt>
DOM Level 2 ECMAScript Binding
</dt>
<dd> <a href="http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html" >http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html</a> </dd>
<dt>
DOM Level 2 Events
</dt>
<dd> <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html" >http://www.w3.org/TR/DOM-Level-2-Events/events.html</a> </dd>
<dt>
DOM Level 2 Style
</dt>
<dd> <a href="http://www.w3.org/TR/DOM-Level-2-Style/" >http://www.w3.org/TR/DOM-Level-2-Style/</a> </dd>
<dt>
DOM Level 3 ECMAScript Binding
</dt>
<dd> <a href="http://www.w3.org/TR/DOM-Level-3-Core/ecma-script-binding.html" >http://www.w3.org/TR/DOM-Level-3-Core/ecma-script-binding.html</a> </dd></dl>
<h4 id='browserResources'>Browser Documentation</h4>
<dl>
<dt>
Mozilla
</dt>
<dd> JavaScript:
<a href="http://developer.mozilla.org/en/docs/JavaScript" >http://developer.mozilla.org/en/docs/JavaScript</a> </dd>
<dd> Gecko DOM Reference:
<a href="http://developer.mozilla.org/en/docs/Gecko_DOM_Reference" >http://developer.mozilla.org/en/docs/Gecko_DOM_Reference</a> </dd>
<dt>
Microsoft
</dt>
<dd> HTML and DHTML Reference:
<a href="http://msdn.microsoft.com/en-us/library/ms533050.aspx" >http://msdn.microsoft.com/en-us/library/ms533050.aspx</a> </dd>
<dd> JScript Language Reference:
<a href="http://msdn.microsoft.com/en-us/library/hbxc2t98%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/hbxc2t98%28VS.85%29.aspx</a> </dd>
<dd> Scripting:
<a href="http://msdn.microsoft.com/en-us/library/ms950396.aspx" >http://msdn.microsoft.com/en-us/library/ms950396.aspx</a> </dd>
<dt>
Opera
</dt>
<dd> Web Specifications Support:
<a href="http://www.opera.com/docs/specs/#ecmascript" >http://www.opera.com/docs/specs/#ecmascript</a> </dd>
<dd> JavaScript Support:
<a href="http://www.opera.com/docs/specs/js/" >http://www.opera.com/docs/specs/js/</a> </dd>
<dd> ECMAScript Support:
<a href="http://www.opera.com/docs/specs/js/ecma" >http://www.opera.com/docs/specs/js/ecma</a> </dd>
<dt>
BlackBerry JavaScript Reference
</dt>
<dd> <a href="http://docs.blackberry.com/en/developers/deliverables/11849/" >http://docs.blackberry.com/en/developers/deliverables/11849/</a> </dd>
<dt>
ICab InScript
</dt>
<dd> <a href="http://www.muchsoft.com/inscript/" >http://www.muchsoft.com/inscript/</a> </dd>
<dt> Apple Safari </dt>
<dd> Web Content Guide:
<a href="http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html" >http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html</a> </dd>
<dt>
Webkit
</dt>
<dd> Project Site: <a href="http://webkit.org/" >http://webkit.org/</a> </dd>
<dd>
Wiki: <a href="http://trac.webkit.org/wiki" >http://trac.webkit.org/wiki</a> </dd>
<dd> DOM Reference:
<a href="http://developer.apple.com/Mac/library/documentation/AppleApplications/Reference/WebKitDOMRef/index.html#//apple_ref/doc/uid/TP40006089" >http://developer.apple.com/Mac/library/documentation/AppleApplications/Reference/WebKitDOMRef/index.html#//apple_ref/doc/uid/TP40006089</a> </dd>
<dt>
Netscape 4 Client-Side JavaScript Reference
</dt>
<dd> <a href="http://docs.sun.com/source/816-6408-10/" >http://docs.sun.com/source/816-6408-10/</a> </dd>
<dt>
Archived documentation for MSIE 3.x
</dt>
<dd> <a href="http://members.tripod.com/%7Ehousten/download/" >http://members.tripod.com/%7Ehousten/download/</a> </dd></dl>
<h4 id='standaloneImplementations'>Standalone ECMAScript Implementations</h4>
<dl>
<dt>
Rhino: An open-source implementation of JavaScript written in Java
</dt>
<dd> <a href="http://www.mozilla.org/rhino/" >http://www.mozilla.org/rhino/</a> </dd>
<dt>
Besen IDE: ECMAScript Edition 5 with IDE
</dt>
<dd> <a href="http://besen.sourceforge.net/" >http://besen.sourceforge.net/</a> </dd>
<dt>
V8: Google's open source JavaScript engine
</dt>
<dd> <a href="http://code.google.com/p/v8/" >http://code.google.com/p/v8/</a> </dd>
<dt>
SpiderMonkey: Mozilla's C implementation of JavaScript
</dt>
<dd> <a href="http://www.mozilla.org/js/spidermonkey/" >http://www.mozilla.org/js/spidermonkey/</a> </dd>
<dt>
Digital Mars DMD Script, console and MS Active Script implementation of ECMAScript
</dt>
<dd> <a href="http://www.digitalmars.com/dscript/" >http://www.digitalmars.com/dscript/</a> </dd></dl>
<h4 id='nonBrowserResources'>Other ECMAScript Implementations</h4>
<dl>
<dt>
Developing Dashboard Widgets: Apple Developer Connection
</dt>
<dd> <a href="http://developer.apple.com/macosx/dashboard.html" >http://developer.apple.com/macosx/dashboard.html</a> </dd>
<dt>
Whitebeam Apache Module: Server Side JavaScript in Apache
</dt>
<dd> <a href="http://www.whitebeam.org/" >http://www.whitebeam.org/</a> </dd></dl></div>
<div id='libraryResources' class='section'><h3>3.3 Javascript Libraries</h3><p>
No javascript libraries are endorsed by this group. If you want help
with using a library, visit that library's discussion group instead.
</p> </div>
<div id='functions' class='section'><h2>4 Functions</h2>
</div><div id='scope' class='section'><h3>4.1 What is (function(){ /*...*/ })() ?</h3><p>
This is an anonymous
<dfn>FunctionExpression</dfn> that is called
immediately after creation.
</p> <p>
Variables declared inside a function are not accessible from
outside the function. This can be useful, for example, to hide
implementation details or to avoid polluting the global scope.
</p><ul class='linkList'><li><a href="http://yura.thinkweb2.com/named-function-expressions/" >http://yura.thinkweb2.com/named-function-expressions/</a></li>
<li><a href="notes/closures/" >notes/closures/</a></li>
<li><a href="http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses" >http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses</a></li>
</ul> </div>
<div id='functionStatement' class='section'><h3>4.2 What is a function statement?</h3><p>
The term
<dfn>function statement</dfn> has been widely and wrongly used to
describe a <code>FunctionDeclaration</code>. This is misleading because in ECMAScript,
a <code>FunctionDeclaration</code> is not a
<dfn>Statement</dfn>; there are places in a program
where a
<dfn>Statement</dfn> is permitted but a <code>FunctionDeclaration</code> is not. To add
to this confusion, some implementations, notably Mozillas', provide a
syntax extension called
<dfn>function statement</dfn>. This is allowed under
section 16 of ECMA-262, Editions 3 and 5.
</p> <p>
Example of nonstandard
<dfn>function statement</dfn>:
</p>
<pre>
// Nonstandard syntax, found in GMail source code. DO NOT USE.
try {
// FunctionDeclaration not allowed in Block.
function Fze(b,a){return b.unselectable=a}
/*...*/
} catch(e) { _DumpException(e) }
</pre>
<p>
Code that uses
<dfn>function statement</dfn> has three known interpretations. Some
implementations process <code>Fze</code> as a
<dfn>Statement</dfn>, in order. Others, including
JScript, evaluate <code>Fze</code> upon entering the execution context that it
appears in. Yet others, notably DMDScript and default configuration of BESEN,
throw a <code>SyntaxError</code>.
</p> <p>
For consistent behavior across implementations, <em>do not use function
statement</em>; use either <code>FunctionExpression</code> or <code>FunctionDeclaration</code> instead.
</p> <p>
Example of <code>FunctionExpression</code> (valid):
</p>
<pre>
var Fze;
try {
Fze = function(b,a){return b.unselectable=a};
/*...*/
} catch(e) { _DumpException(e) }
</pre>
<p>
Example of <code>FunctionDeclaration</code> (valid):
</p>
<pre>
// Program code
function aa(b,a){return b.unselectable=a}
</pre>
<p>
<!--
Notable examples of the misuse of the term "function statement"
can be seen in David Flanagan's "JavaScript: The Definitive Guide",
Douglas Crockford's "JavaScript: The Good Parts", MDC documentation,
JSLint error messages.
--></p><ul class='linkList'><li><a href="example/functionStatement.html" >example/functionStatement.html</a></li>
<li><a href="https://mail.mozilla.org/pipermail/es-discuss/2008-February/005314.html" >https://mail.mozilla.org/pipermail/es-discuss/2008-February/005314.html</a></li>
<li><a href="http://groups.google.com/group/comp.lang.javascript/msg/aa9a32d0c6ae0342" >http://groups.google.com/group/comp.lang.javascript/msg/aa9a32d0c6ae0342</a></li>
<li><a href="http://groups.google.com/group/comp.lang.javascript/msg/3987eac87ad27966" >http://groups.google.com/group/comp.lang.javascript/msg/3987eac87ad27966</a></li>
<li><a href="http://nanto.asablo.jp/blog/2005/12/10/172622" >http://nanto.asablo.jp/blog/2005/12/10/172622</a></li>
<li> (Article in Japanese)
</li>
</ul> </div>
<div id='dates' class='section'><h2>5 Dates</h2>
<p>
ISO 8601 defines date and time formats. Some benefits include:
</p>
<ul>
<li> language-independent and unambiguous world-wide </li>
<li> sortable with a trivial string comparison </li>
<li> easily readable and writable by software </li>
<li> compatible with standards ISO 9075 and <a href="http://www.ietf.org/rfc/rfc3339.txt" >rfc 3339</a> </li></ul><p>
The ISO Extended format for common date is <code>YYYY-MM-DD</code>, and for time is
<code>hh:mm:ss</code>.
</p> <p>
For an event with an offset from UTC, use <code>YYYY-MM-DDThh:mm:ss±hh:mm</code>.
</p> <p>
Never use a local date/time format for a non-local event. Instead, use
UTC, as in <code>YYYY-MM-DDThh:mm:ssZ</code> (<code>Z</code> is the only letter suffix).
</p> <p>
The <code>T</code> can be omitted where that would not cause ambiguity. For
rfc 3339 compliance, it may be replaced by a space and for SQL,
it <em>must</em> be replaced by a single space.
</p> <p>
Year <code>0000</code> is unrecognized by some formats (XML Schema, <code>xs:date</code>).
</p><ul class='linkList'><li><a href="#onlineResources" >ECMA-262 Date.prototype, s. 15.9</a></li>
<li><a href="http://www.cl.cam.ac.uk/~mgk25/iso-time.html" >A summary of the international standard date and time notation, by Markus Kuhn</a></li>
<li><a href="http://en.wikipedia.org/wiki/ISO_8601" >http://en.wikipedia.org/wiki/ISO_8601</a></li>
<li><a href="res/ISO_8601-2004_E.pdf" >ISO 8601:2004(E)</a></li>
<li><a href="http://www.w3.org/QA/Tips/iso-date" >W3C QA Tip: Use international date format (ISO)</a></li>
<li><a href="http://www.ietf.org/rfc/rfc3339.txt" >RFC 3339, Date and Time on the Internet: Timestamps
</a></li>
<li><a href="http://www.w3.org/TR/xmlschema-2/#dateTime" >http://www.w3.org/TR/xmlschema-2/#dateTime</a></li>
</ul> </div><div id='formatDate' class='section'><h3 id='FAQ4_30'>5.1 How do I format a Date object with javascript?</h3><p>
A local <code>Date</code> object where <code>0 <= year <= 9999</code> can be
formatted to a common ISO 8601 format <code>YYYY-MM-DD</code> with:-
</p>
<pre>
/** Formats a Date to YYYY-MM-DD (local time), compatible with both
* ISO 8601 and ISO/IEC 9075-2:2003 (E) (SQL 'date' type).
* @param {Date} dateInRange year 0000 to 9999.
* @throws {RangeError} if the year is not in range
*/
function formatDate(dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
if(!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return yyyy + "-" + mm + "-" + dd;
}
</pre>
<ul class='linkList'><li><a href="http://www.merlyn.demon.co.uk/js-date9.htm" >http://www.merlyn.demon.co.uk/js-date9.htm</a></li>
</ul> </div>
<div id='parseDate' class='section'><h3>5.2 How can I create a Date object from a String?</h3><p>
An Extended ISO 8601 local Date format <code>YYYY-MM-DD</code> can be parsed to a
Date with the following:-
</p>
<pre>
/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/
function parseISO8601(dateStringInRange) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);
if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}</pre>
</div>
<div id='numbers' class='section'><h2>6 Numbers</h2>
</div><div id='formatNumber' class='section'><h3 id='FAQ4_6'>6.1 How do I format a Number as a String with exactly 2 decimal places?</h3><p>
When formatting money for example, to format 6.57634 to 6.58, 6.7 to
6.50, and 6 to 6.00?
</p> <p>
Rounding of x.xx5 is unreliable, as most numbers are not represented
exactly. See also:
<a href="#binaryNumbers" >Why does simple decimal arithmetic give strange results?</a></p> <p>
The statement <code>n = Math.round(n * 100)/100</code> converts <code>n</code> to a <code>Number</code> value
close to a multiple of <code>0.01</code>. However, there are some problems.
Converting the number to a string <code>(n + "")</code>, does not give
trailing zeroes. Rounding numbers that are very close to <code>x.5</code>, for example,
<code>Math.round(0.49999999999999992)</code> results <code>1</code>.
</p> <p>
ECMA-262 3rd Edition introduced <code>Number.prototype.toFixed</code>.
There are bugs in JScript 5.8 and below with certain numbers, for example
<code>0.007.toFixed(2)</code> incorrectly results <code>0.00</code>.
</p> <p>
</p>
<pre>
var numberToFixed =
(function() {
return toFixedString;
function toFixedString(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}
function toUnsignedString(m, digits) {
var t, s = Math.round(m * Math.pow(10, digits)) + "",
start, end;
if (/\D/.test(s)) {
return "" + m;
}
s = padLeft(s, 1 + digits, "0");
start = s.substring(0, t = (s.length - digits));
end = s.substring(t);
if(end) {
end = "." + end;
}
return start + end; // avoid "0."
}
/**
* @param {string} input: input value converted to string.
* @param {number} size: desired length of output.
* @param {string} ch: single character to prefix to s.
*/
function padLeft(input, size, ch) {
var s = input + "";
while(s.length < size) {
s = ch + s;
}
return s;
}
})();
// Test results
document.writeln([
"numberToFixed(9e-3, 12) => " + numberToFixed(9e-3, 12),
"numberToFixed(1.255, 2) => " + numberToFixed(1.255, 2),
"numberToFixed(1.355, 2) => " + numberToFixed(1.355, 2),
"numberToFixed(0.1255, 3) => " + numberToFixed(0.1255, 3),
"numberToFixed(0.07, 2) => " + numberToFixed(0.07, 2),
"numberToFixed(0.0000000006, 1) => " + numberToFixed(0.0000000006, 1),
"numberToFixed(0.0000000006, 0) => " + numberToFixed(0.0000000006, 0)
].join("\n"));
</pre>
<ul class='linkList'><li><a href="http://www.merlyn.demon.co.uk/js-round.htm" >http://www.merlyn.demon.co.uk/js-round.htm</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/sstyff0z%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/sstyff0z%28VS.85%29.aspx</a></li>
</ul> </div>
<div id='binaryNumbers' class='section'><h3 id='FAQ4_7'>6.2 Why does simple decimal arithmetic give strange results?</h3><p>
For example, <code>5 * 1.015</code> does not give exactly
<code>5.075</code> and <code>0.06+0.01</code> does
not give exactly <code>0.07</code> in javascript.
</p> <p>
ECMAScript numbers are represented in binary as IEEE-754 (IEC 559)
Doubles, with a resolution of 53 bits, giving an accuracy of
15-16 decimal digits; integers up to just over <code>9e15</code> are
precise, but few decimal fractions are. Given this, arithmetic
is as exact as possible, but no more. Operations on integers
are exact if the true result and all intermediates are integers
within that range.
</p> <p>
In particular, non-integer results should not normally be
compared for equality; and non-integer computed results
commonly need rounding; see <a href="#formatNumber" >How do I format a Number as a String with exactly 2 decimal places?</a></p><ul class='linkList'><li><a href="http://msdn.microsoft.com/en-us/library/7wkd9z69%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/7wkd9z69%28VS.85%29.aspx</a></li>
<li><a href="http://www.merlyn.demon.co.uk/js-misc0.htm#DW4" >http://www.merlyn.demon.co.uk/js-misc0.htm#DW4</a></li>
</ul> <p>
Otherwise, use <code>Math.round</code> on the results of expressions which
should be of integer value.
</p> </div>
<div id='parseIntBase' class='section'><h3 id='FAQ4_12'>6.3 Why does K = parseInt('09') set K to 0?</h3><p>
Method <code>parseInt</code> generally needs a second parameter, <code>radix</code>,
for the base (from 2 to 36).
</p> <p>
If <code>radix</code> is omitted, the base is determined by the contents of
the string. Any string beginning with <code>'0x'</code> or <code>'0X'</code> represents a
hexadecimal number. A string beginning with a leading 0 may be parsed as
octal (as if <code>raxix</code> were 8), in ECMA-262 Ed 3 (octal digits are <code>0-7</code>).
If string <code>'09'</code> is converted to <code>0</code>.
</p> <p>
To force use of a particular base, use the <code>radix</code>
parameter: <code>parseInt("09", base)</code>.
<!-- [omit]
If base 10 is desired,
the unary <ICODE>+</ICODE> operator can be an option. Example:
<ICODE>
var s = '-09.1'; // Input string.
var j = +s; // Convert to number. Result: -9.1
var n = j|0; // Chop off decimal (convert ToInt32). Result: -9
</ICODE>
[/omit] --></p> <p>
</p><ul class='linkList'><li><a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt" >http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/x53yedee%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/x53yedee%28VS.85%29.aspx</a></li>
<li><a href="http://docs.sun.com/source/816-6408-10/toplev.htm#1064173" >http://docs.sun.com/source/816-6408-10/toplev.htm#1064173</a></li>
<li><a href="notes/type-conversion/#tcPrIntRx" >notes/type-conversion/#tcPrIntRx</a></li>
</ul> </div>
<div id='typeConvert' class='section'><h3>6.4 Why does 1+1 equal 11? or How do I convert a string to a number?</h3><p>
Variables are not typed; their values are. The conversion between a
string and a number happens automatically.
</p> <p>
The addition operator (<code>+</code>) performs concatenation if either operand is a
string, thus <code>"1" + 1</code> results <code>"11"</code>. To perform addition, you might need
to first convert the string to a number. For example <code>+varname</code> or
<code>Number(varname)</code> or <code>parseInt(varname, 10)</code> or
<code>parseFloat(varname)</code>. Form control values are strings, as is the result
from a <code>prompt</code> dialog. Convert these to numbers before performing
addition: <code>+'1' + 1</code> results <code>2</code>.
</p><ul class='linkList'><li>
Additional Notes: </li>
<li><a href="notes/type-conversion/" >notes/type-conversion/</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/67defydd%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/67defydd%28VS.85%29.aspx</a></li>
</ul> </div>
<div id='randomNumber' class='section'><h3 id='FAQ4_22'>6.5 How do I generate a random integer from 1 to n?</h3><p>
<code>Math.random()</code> returns a value <code>R</code> such that <code>0 <= R < 1.0</code>; therefore:
</p>
<pre>
// positive integer expected
function getRandomNumber(n) {
return Math.floor(n * Math.random());
}
</pre>
<p>
- gives an evenly distributed random integer in the range from
<code>0</code> to <code>n - 1</code> inclusive; use <code>getRandomNumber(n)+1</code> for <code>1</code> to <code>n</code>.
</p><ul class='linkList'><li><a href="http://msdn.microsoft.com/en-us/library/41336409%28VS.85%29.aspx" >http://msdn.microsoft.com/en-us/library/41336409%28VS.85%29.aspx</a></li>
<li><a href="http://docs.sun.com/source/816-6408-10/math.htm" >http://docs.sun.com/source/816-6408-10/math.htm</a></li>
<li>
How to Deal and Shuffle, see in: </li>
<li><a href="http://www.merlyn.demon.co.uk/js-randm.htm" >http://www.merlyn.demon.co.uk/js-randm.htm</a></li>
</ul> </div>
<div id='objects' class='section'><h2>7 Objects</h2>
</div><div id='nativeObject' class='section'><h3>7.1 What is a native object?</h3><p>
A
<dfn>native object</dfn> is any object whose semantics are fully defined by
ECMA-262.
</p> <p>
Some
<dfn>native objects</dfn> are
<dfn>built-in</dfn>; others, such as
<dfn>user-defined</dfn> objects,
may be constructed during the execution of an ECMAScript program.
</p> <p>
Example:
</p>
<pre>
// Native built-in objects:
var m = Math, // Built-in Math object.
slice = Array.prototype.slice, // Built-in native method.
o = {}, // Native user-defined object.
f = function(){}, // Native user-defined function.
d = new Date(),
a = [],
e = new Error("My Message.");
</pre>
<p>
See also:
</p><ul class='linkList'><li><a href="http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/" >http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/</a></li>
</ul> </div>
<div id='builtInObject' class='section'><h3>7.2 What is a built-in object?</h3><p>
A
<dfn>built-in</dfn> object is any object supplied by an ECMAScript
implementation, independent of the host environment, that is present
at the start of the execution of an ECMAScript program.
</p> <p>
ECMA-262 3rd Edition defines the following
<dfn>built-in</dfn> objects:
</p>
<dl id='builtInsList'>
<dt> Objects </dt>
<dd> <em>global</em>, Math </dd>
<dt> Constructors </dt>
<dd> Object, Function, Array, String, Boolean, Number, Date, RegExp </dd>
<dt> Errors </dt>
<dd> Error, Date, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError </dd>
<dt> Functions </dt>
<dd> eval, parseInt, parseFloat, isNaN, isFinite, decodeURI,
decodeURIComponent, encodeURI, encodeURIComponent </dd></dl><p>
ECMA-262 Edition 5 defines also the built-in object <code>JSON</code>.
</p> <p>
Nonstandard
<dfn>built-in</dfn> objects may include <code>RuntimeObject</code>,
<code>String.prototype.link</code>, <code>CollectGarbage</code>, and more.
</p> </div>
<div id='hostObject' class='section'><h3 id='FAQ2_8'>7.3 What is a host object?</h3><p>
A
<dfn>host object</dfn> is any object supplied by the host environment to