30,99 €
The bestselling JavaScript guide, updated with current features and best practices Beginning JavaScript 5th Edition shows you how to work effectively with JavaScript frameworks, functions, and modern browsers, and teaches more effective coding practices using HTML5. This new edition has been extensively updated to reflect the way JavaScript is most commonly used today, introducing you to the latest tools and techniques available to JavaScript developers. Coverage includes modern coding practices using HTML5 markup, the JSON data format, DOM APIs, the jQuery framework, and more. Exercises with solutions provide plenty of opportunity to practice, and the companion website offers downloadable code for all examples given in the book. * Learn JavaScript using the most up to date coding style * Understand JSON, functions, events, and feature detection * Utilize the new HTML5 elements and the related API * Explore new features including geolocation, local storage, and more JavaScript has shaped the Web from a passive medium into one that is rich, dynamic, and interactive. No matter the technology on the server side, it's JavaScript that makes it come alive in the browser. To learn JavaScript the way it's used today, Beginning JavaScript, 5th Edition is your concise guide.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 1036
Veröffentlichungsjahr: 2015
INTRODUCTION
WHO THIS BOOK IS FOR
WHAT THIS BOOK COVERS
WHAT YOU NEED TO USE THIS BOOK
CONVENTIONS
SOURCE CODE
ERRATA
P2P.WROX.COM
CHAPTER 1: INTRODUCTION TO JAVASCRIPT AND THE WEB
INTRODUCTION TO JAVASCRIPT
WHERE DO MY SCRIPTS GO?
YOUR FIRST SIMPLE JAVASCRIPT PROGRAM
WRITING MORE JAVASCRIPT
A BRIEF LOOK AT BROWSERS AND COMPATIBILITY PROBLEMS
SUMMARY
CHAPTER 2: DATA TYPES AND VARIABLES
TYPES OF DATA IN JAVASCRIPT
VARIABLES—STORING DATA IN MEMORY
USING DATA—CALCULATIONS AND BASIC STRING MANIPULATION
DATA TYPE CONVERSION
ARRAYS
SUMMARY
EXERCISES
CHAPTER 3: DECISIONS AND LOOPS
DECISION MAKING—THE IF AND SWITCH STATEMENTS
LOOPING—THE FOR AND WHILE STATEMENTS
SUMMARY
EXERCISES
CHAPTER 4: FUNCTIONS AND SCOPE
CREATING YOUR OWN FUNCTIONS
SCOPE AND LIFETIME
FUNCTIONS AS VALUES
SUMMARY
EXERCISES
CHAPTER 5: JAVASCRIPT—AN OBJECT-BASED LANGUAGE
OBJECT-BASED PROGRAMMING
JAVASCRIPT’S NATIVE OBJECT TYPES
CREATING YOUR OWN CUSTOM OBJECTS
CREATING NEW TYPES OF OBJECTS (REFERENCE TYPES)
SUMMARY
EXERCISES
CHAPTER 6: STRING MANIPULATION
ADDITIONAL STRING METHODS
REGULAR EXPRESSIONS
THE STRING OBJECT
USING THE REGEXP OBJECT’S CONSTRUCTOR
SUMMARY
EXERCISES
CHAPTER 7: DATE, TIME, AND TIMERS
WORLD TIME
TIMERS IN A WEB PAGE
SUMMARY
EXERCISES
CHAPTER 8: PROGRAMMING THE BROWSER
INTRODUCTION TO THE BROWSER’S OBJECTS
DETERMINING THE USER’S BROWSER
SUMMARY
EXERCISES
CHAPTER 9: DOM SCRIPTING
THE WEB STANDARDS
THE DOCUMENT OBJECT MODEL
MANIPULATING THE DOM
SUMMARY
EXERCISES
CHAPTER 10: EVENTS
TYPES OF EVENTS
CONNECTING CODE TO EVENTS
THE STANDARD EVENT MODEL
EVENT HANDLING IN OLD VERSIONS OF INTERNET EXPLORER
WRITING CROSS-BROWSER CODE
NATIVE DRAG AND DROP
SUMMARY
EXERCISES
CHAPTER 11: HTML FORMS: INTERACTING WITH THE USER
HTML FORMS
TRADITIONAL FORM OBJECT PROPERTIES AND METHODS
HTML5 FORM OBJECT PROPERTIES AND METHODS
SUMMARY
EXERCISES
CHAPTER 12: JSON
XML
JSON
SUMMARY
EXERCISES
CHAPTER 13: DATA STORAGE
BAKING YOUR FIRST COOKIE
CREATING A COOKIE
GETTING A COOKIE’S VALUE
COOKIE LIMITATIONS
COOKIE SECURITY AND IE
WEB STORAGE
SUMMARY
EXERCISES
CHAPTER 14: AJAX
WHAT IS AJAX?
USING THE XMLHTTPREQUEST OBJECT
CREATING A SIMPLE AJAX MODULE
VALIDATING FORM FIELDS WITH AJAX
THINGS TO WATCH OUT FOR
SUMMARY
EXERCISES
CHAPTER 15: HTML5 MEDIA
A PRIMER
SCRIPTING MEDIA
SUMMARY
EXERCISES
CHAPTER 16: JQUERY
GETTING JQUERY
jQUERY’S API
SUMMARY
EXERCISES
CHAPTER 17: OTHER JAVASCRIPT LIBRARIES
DIGGING INTO MODERNIZR
DIVING INTO PROTOTYPE
DELVING INTO MOOTOOLS
SUMMARY
EXERCISES
CHAPTER 18: COMMON MISTAKES, DEBUGGING, AND ERROR HANDLING
D’OH! I CAN’T BELIEVE I JUST DID THAT: SOME COMMON MISTAKES
ERROR HANDLING
DEBUGGING
SUMMARY
EXERCISES
APPENDIX A: ANSWERS TO EXERCISES
CHAPTER 2
CHAPTER 3
CHAPTER 4
CHAPTER 5
CHAPTER 6
CHAPTER 7
CHAPTER 8
CHAPTER 9
CHAPTER 10
CHAPTER 11
CHAPTER 12
CHAPTER 13
CHAPTER 14
CHAPTER 15
CHAPTER 16
CHAPTER 17
CHAPTER 18
APPENDIX B: JAVASCRIPT CORE REFERENCE
BROWSER REFERENCE
RESERVED WORDS
JAVASCRIPT OPERATORS
JAVASCRIPT STATEMENTS
TOP-LEVEL PROPERTIES AND FUNCTIONS
JAVASCRIPT CORE OBJECTS
APPENDIX C: W3C DOM REFERENCE
DOM CORE OBJECTS
HTML DOM OBJECTS
DOM EVENT MODEL AND OBJECTS
MISCELLANEOUS EVENTS
APPENDIX D: LATIN-1 CHARACTER SET
TITLE PAGE
COPYRIGHT
DEDICATION
CREDITS
ABOUT THE AUTHORS
ACKNOWLEDGMENTS
ADVERT
EULA
Chapter 1
Figure 1.1
Figure 1.2
Figure 1.3
Figure 1.4
Chapter 2
Figure 2.1
Figure 2.2
Figure 2.3
Figure 2.4
Figure 2.5
Figure 2.6
Figure 2.7
Figure 2.8
Figure 2.9
Figure 2.10
Figure 2.11
Chapter 3
Figure 3.1
Figure 3.2
Figure 3.3
Figure 3.4
Figure 3.5
Figure 3.6
Figure 3.7
Figure 3.8
Figure 3.9
Figure 3.10
Figure 3.11
Figure 3.12
Chapter 4
Figure 4.1
Figure 4.2
Chapter 5
Figure 5.1
Figure 5.2
Figure 5.3
Figure 5.4
Figure 5.5
Figure 5.6
Chapter 6
Figure 6.1
Figure 6.2
Figure 6.3
Figure 6.4
Figure 6.5
Figure 6.6
Figure 6.7
Figure 6.8
Figure 6.9
Figure 6.10
Chapter 7
Figure 7.1
Figure 7.2
Figure 7.3
Chapter 8
Figure 8.1
Figure 8.2
Figure 8.3
Figure 8.4
Chapter 9
Figure 9.1
Figure 9.2
Figure 9.3
Figure 9.4
Figure 9.5
Figure 9.6
Figure 9.7
Figure 9.8
Figure 9.9
Figure 9.10
Figure 9.11
Chapter 10
Figure 10.1
Figure 10.2
Figure 10.3
Figure 10.4
Figure 10.5
Figure 10.6
Figure 10.7
Figure 10.8
Figure 10.9
Chapter 11
Figure 11.1
Figure 11.2
Figure 11.3
Figure 11.4
Figure 11.5
Figure 11.6
Figure 11.7
Figure 11.8
Figure 11.9
Figure 11.10
Chapter 13
Figure 13.1
Figure 13.2
Figure 13.3
Figure 13.4
Figure 13.5
Figure 13.6
Figure 13.7
Figure 13.8
Figure 13.9
Figure 13.10
Figure 13.11
Figure 13.12
Figure 13.13
Figure 13.14
Figure 13.15
Figure 13.16
Figure 13.17
Figure 13.18
Figure 13.19
Figure 13.20
Chapter 14
Figure 14.1
Figure 14.2
Figure 14.3
Figure 14.4
Figure 14.5
Figure 14.6
Figure 14.7
Chapter 15
Figure 15.1
Figure 15.2
Figure 15.3
Figure 15.4
Figure 15.5
Figure 15.6
Figure 15.7
Chapter 17
Figure 17.1
Figure 17.2
Chapter 18
Figure 18.1
Figure 18.2
Figure 18.3
Figure 18.4
Figure 18.5
Figure 18.6
Figure 18.7
Figure 18.8
Figure 18.9
Figure 18.10
Figure 18.11
Figure 18.12
Figure 18.13
Figure 18.14
Figure 18.15
Figure 18.16
Figure 18.17
Figure 18.18
Figure 18.19
Figure 18.20
Figure 18.21
Figure 18.22
Figure 18.23
Figure 18.24
Figure 18.25
Figure 18.26
Figure 18.27
Figure 18.28
Figure 18.29
Figure 18.30
Figure 18.31
Cover
Table of Contents
Chapter 1
xix
xx
xxi
xxii
xxiii
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
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
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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
295
296
297
298
299
300
301
302
303
304
305
307
308
309
310
311
312
313
314
315
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
391
392
393
394
395
396
397
398
399
400
403
404
405
407
408
409
410
411
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
435
436
438
439
440
441
442
443
444
445
446
447
449
450
451
452
453
454
455
456
457
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
505
506
507
508
509
510
511
512
514
515
516
517
518
519
520
521
522
523
524
525
526
529
530
531
532
533
534
535
536
537
538
539
540
541
542
545
546
547
549
550
551
552
553
554
555
556
557
558
560
561
562
563
564
565
566
567
568
569
571
572
573
574
576
577
578
579
580
582
583
584
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
611
612
613
616
617
618
619
621
622
623
624
625
626
627
628
631
632
633
634
636
637
638
639
641
642
645
646
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
671
672
673
674
676
677
678
680
681
682
683
684
685
686
687
689
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
723
724
725
726
727
728
729
730
JAVASCRIPT IS A SCRIPTING LANGUAGE that enables you to enhance static web applications by providing dynamic, personalized, and interactive content. This improves the experience of visitors to your site and makes it more likely that they will visit again. You must have seen the flashy drop-down menus, moving text, and changing content that are now widespread on websites—they are enabled through JavaScript. Supported by all the major browsers, JavaScript is the language of choice on the web. It can even be used outside web applications—to automate administrative tasks, for example.
This book aims to teach you all you need to know to start experimenting with JavaScript: what it is, how it works, and what you can do with it. Starting from the basic syntax, you’ll move on to learn how to create powerful web applications. Don’t worry if you’ve never programmed before—this book will teach you all you need to know, step by step. You’ll find that JavaScript can be a great introduction to the world of programming: with the knowledge and understanding that you’ll gain from this book, you’ll be able to move on to learn newer and more advanced technologies in the world of computing.
To get the most out of this book, you’ll need to have an understanding of HTML, CSS, and how to create a static web page. You don’t need to have any programming experience.
This book will also suit you if you have some programming experience already and would like to turn your hand to web programming. You will know a fair amount about computing concepts, but maybe not as much about web technologies.
Alternatively, you may have a design background and know relatively little about the web and computing concepts. For you, JavaScript will be a cheap and relatively easy introduction to the world of programming and web application development.
Whoever you are, I hope that this book lives up to your expectations.
You’ll begin by looking at exactly what JavaScript is, and taking your first steps with the underlying language and syntax. You’ll learn all the fundamental programming concepts, including data and data types, and structuring your code to make decisions in your programs or to loop over the same piece of code many times.
Once you’re comfortable with the basics, you’ll move on to one of the key ideas in JavaScript—the object. You’ll learn how to take advantage of the objects that are native to the JavaScript language, such as functions, dates, and strings, and find out how these objects enable you to manage complex data and simplify your programs. Next, you’ll see how you can use JavaScript to manipulate and detect objects made available to you in the browser and detect the browsers.
From here, you’ll move on to more advanced topics, such as writing code to dynamically manipulate elements within a web page and executing code when certain things happen within your page. You’ll also learn how to script forms and other controls. Using this knowledge, you can start to create truly professional-looking applications that enable you to interact with the user.
You’ll then learn how to store data within the browser and communicate directly with a server. You’ll also learn how to write code for the new HTML5 media elements, and write your own custom user interface for them.
You’ll explore some of the time saving JavaScript frameworks such as jQuery, Modernizr, Prototype, and MooTools and see how they work and how they can help you create sophisticated JavaScript powered applications.
Finally, you’ll look at common syntax and logical errors, how you can spot them, and how to use the JavaScript debuggers for Chrome, Internet Explorer, Firefox, Safari, and Opera to aid you with this task. Also, you need to examine how to handle the errors that slip through the net, and ensure that these do not detract from the experience of the end user of your application.
All the new concepts introduced in this book will be illustrated with practical examples, which enable you to experiment with JavaScript and build on the theory that you have just learned.
You’ll find four appendixes at the end of the book. Appendix A provides solutions to the exercises included at the end of most chapters throughout the book. The remaining appendixes contain the reference material that your authors hope you find useful and informational. Appendix B contains the JavaScript language’s core reference. Appendix C contains a complete W3C DOM Core reference—as well as information on the HTML DOM and DOM level 2 Event model. Appendix D contains the decimal and hexadecimal character codes for the Latin-1 character set.
Because JavaScript is a text-based technology, all you really need to create documents containing JavaScript is a text editor. Any will do.
Also, in order to try out the code in this book, you will need a web browser that supports a modern version of JavaScript. Ideally, this means the latest versions of Chrome, Internet Explorer, Firefox, Safari, and Opera. The book has been extensively tested with these browsers. However, the code should work in any modern web browser. Where there are exceptions, they will be clearly noted.
To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.
The Try It Out is an exercise you should work through, following the text in the book.
It usually consists of a set of steps.
Each step has a number.
Follow the steps with your copy of the database.
As you work through each Try It Out, the code you’ve typed will be explained in detail.
WARNING Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.
NOTE Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
As for styles in the text:
We
highlight in italic type
new terms and important words when we introduce them.
We show keyboard strokes like this: Ctrl+A.
We show file names, URLs, and code within the text like so:
persistence.properties.
We present code in two different ways:
Important code in code examples is highlighted with a gray background.
The gray highlighting is not used for code that’s less important in the present context, or that has been shown before.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source-code files that accompany the book. All of the source code used in this book is available for download at www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book.
NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-118-90333-9.
Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.
You can also view the examples presented in this book at http://beginningjs.com.
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time you will be helping us provide even higher-quality information.
To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list, including links to each book’s errata, is also available at www.wrox.com/misc-pages/booklist.shtml.
If you don’t spot “your” error on the Book Errata page go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system on which you can post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
Go to
p2p.wrox.com
and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join as well as any optional information you wish to provide, and click Submit.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
NOTE You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
WHAT YOU WILL LEARN IN THIS CHAPTER:
Adding JavaScript to your web pages
Referencing external JavaScript files
Changing the background color of a web page
WROX.COM CODE DOWNLOADS FOR THIS CHAPTER
You can find the wrox.com code downloads for this chapter at http://www.wiley.com/go/BeginningJavaScript5E on the Download Code tab. You can also view all of the examples and related files at http://beginningjs.com.
In this introductory chapter, you look at what JavaScript is, what it can do for you, and what you need in order to use it. With these foundations in place, you will see throughout the rest of the book how JavaScript can help you to create powerful web applications for your website.
The easiest way to learn something is by actually doing it, so throughout the book you create a number of useful example programs using JavaScript. This process starts in this chapter, by the end of which you will have created your first piece of JavaScript code.
In this section you take a brief look at what JavaScript is, where it came from, how it works, and what sorts of useful things you can do with it.
Having bought this book, you are probably already well aware that JavaScript is some sort of , but what is a computer language? Put simply, a computer language is a series of instructions that tell the computer to do something. That something can be one of a wide variety of things, including displaying text, moving an image, or asking the user for information. Normally, the instructions, or what is termed , are from the top line downward. This simply means that the computer looks at the code you’ve written, works out what action you want it to take, and then takes that action. The act of processing the code is called or it.
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!