HTML5 Programming with JavaScript® For Dummies®

Published by
John Wiley & Sons, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com

Copyright © 2013 by John Wiley & Sons, Inc., Hoboken, New Jersey

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Trademarks: Wiley, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. JavaScript is a registered trademark of Oracle America,Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2013936244

ISBN 978-1-118-43166-5 (pbk); ISBN 978-1-118-46209-6 (ebk); ISBN 978-1-118-61188-3 (ebk); ISBN 978-1-118-49418-9 (ebk)

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

About the Author

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 91 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include Windows command-line references, books on VBA and Visio, several books on C#, and an IronPython programmer's guide. His technical editing skills have helped more than 63 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He's also contributed articles to magazines like such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John's blog at http://blog.johnmuellerbooks.com.

When John isn't working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com. Feel free to take a look and make suggestions on how he can improve it.

Dedication

Dedicated to people who have given me hope and who have helped me realize new potential as an author; on the occasion of their 50th anniversary, Bill and Karen Bridges.

Author’s Acknowledgments

Thanks to my wife, Rebecca, for working with me to get this book completed. I really don’t know what I would have done without her help in researching and compiling some of the information that appears in this book. She also did a fine job of proofreading my rough draft. Rebecca keeps the house running while I’m buried in work.

Russ Mullen deserves thanks for his technical edit of this book. He greatly added to the accuracy and depth of the material you see here. Russ is always providing me with great URLs for new products and ideas. However, it’s the testing Russ does that helps most. He’s the sanity check for my work. Russ also has different computer equipment than mine, so he’s able to point out flaws that I might not otherwise notice.

Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t really consider. I always appreciate his assistance. It’s good to know that someone wants to help.

A number of people read all or part of this book to help me refine the approach, test the coding examples, and generally provide input that all readers wish they could have. These unpaid volunteers helped in ways too numerous to mention here. I especially appreciate the efforts of Eva Beattie, Glenn Russell, Osvaldo Téllez Almirall, and Gerald Wilson, Jr., who provided general input, read the entire book, and selflessly devoted themselves to this project.

Finally, I would like to thank Kim Darosett, Katie Feltman, Virginia Sanders, Katie Crocker, and the rest of the editorial and production staff at Wiley for their assistance in bringing this book to print. It’s always nice to work with such a great group of professionals.

Publisher’s Acknowledgments

We're proud of this book; please send us your comments at http://dummies.custhelp.com. For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

Some of the people who helped bring this book to market include the following:

Acquisitions and Editorial

Senior Project Editor: Kim Darosett

Acquisitions Editor: Constance Santisteban

Copy Editor: Virginia Sanders

Technical Editor: Russ Mullen

Editorial Manager: Leah Michael

Editorial Assistant: Annie Sullivan

Senior Editorial Assistant: Cherie Case

Cover Photo: © kertlis/iStockphoto

Composition Services

Project Coordinator: Katherine Crocker

Layout and Graphics: Amy Hassos, Joyce Haughey

Proofreaders: Jessica Kramer, Lisa Stiers

Indexer: Ty Koontz

Publishing and Editorial for Technology Dummies

Richard Swadley, Vice President and Executive Group Publisher

Andy Cummings, Vice President and Publisher

Mary Bednarek, Executive Acquisitions Director

Mary C. Corder, Editorial Director

Publishing for Consumer Dummies

Kathleen Nebenhaus, Vice President and Executive Publisher

Composition Services

Debbie Stailey, Director of Composition Services

HTML5 Programming with JavaScript® For Dummies®

Visit www.dummies.com/cheatsheet/html5programmingwithjavascript to view this book's cheat sheet.

Table of Contents

Introduction

About This Book

What You Don’t Have to Read

Foolish Assumptions

Conventions Used in This Book

How This Book Is Organized

Part I: Understanding the Basics of JavaScript

Part II: Speaking the JavaScript Language

Part III: Meeting JavaScript’s Control Structures

Part IV: Interacting with Users and HTML

Part V: Extending JavaScript Further

Part VI: The Part of Tens

The companion Website

Icons Used in This Book

Where to Go from Here

Part I: Understanding the Basics of JavaScript

Chapter 1: HTML, Say Hello to JavaScript

Introducing JavaScript

Java and JavaScript aren’t long-lost relatives

Recognizing the benefits of JavaScript

Seeing How JavaScript Fits into an HTML Document

Starting an HTML5 document

Understanding the alert() function

Using the <script> tag

Placing the code in the page heading

Relying on external files

Chapter 2: Assessing Tools of the Trade

Researching Browsers and Their Debugging Tools

Catching up with Firefox

Meeting Google Chrome

Checking out native browsers: Internet Explorer or Safari

Checking browser and version

Discovering Programs to Write JavaScript

Using a text editor

Using a dedicated editor

Hosting Your Site

Quickly looking at Web hosting

Using JavaScript from your computer

Testing your installation

Chapter 3: Integrating HTML5 and JavaScript

Creating Simple Output

Writing to an HTML element

Creating direct document output

Avoiding potential problems with output

Working with JavaScript Statements

Understanding the dot syntax

Adding multiple statements to a single script

Defining basic code blocks

Understanding case sensitivity

Using white space effectively

Using the line continuation character

Writing Comments in JavaScript

Creating single-line comments

Creating multi-line comments

Preventing code execution by using comments

Alerting Visitors That JavaScript Has Something to Say

Using the <noscript> tag

Using styles

Part II: Speaking the JavaScript Language

Chapter 4: Embracing JavaScript Variables

Understanding Simple Variables

Seeing variables as storage bins

Declaring variables

Discovering variable types

Understanding undefined and null variables

Working with Booleans

Working with Numbers

Understanding the operators

Doing simple math

Changing number formats

Working with Text

Concatenating text

Changing word and letter formats

Working with Arrays

Creating an array

Accessing array members

Chapter 5: Working with Objects

Defining Objects

Starting with the Object object

Understanding properties

Understanding methods

Understanding events

Using and Creating Objects

Using JavaScript objects

Building custom objects

Working with Object Literals

Creating the name/value pairs

Adding new name/value pairs

Naming Variables and Objects Properly

Chapter 6: Getting to Know the Standard JavaScript Objects

Defining the Basic JavaScript Objects

Understanding Object

Understanding Number

Understanding String

Understanding Date

Understanding RegExp

Introducing the String Object

Manipulating and searching strings

Working with regular expressions

Working with the Date Object

Getting today’s date right

Calculating differences in dates

Part III: Meeting JavaScript’s Control Structures

Chapter 7: Using Functions

Using the Built-In Functions

Working with the Math Object

Building Custom Functions

Reducing work using functions

Passing parameters to functions

Creating functions with a return value

Creating functions with optional and required arguments

Understanding variable scope

Working with Private Properties and Methods

Chapter 8: Making Choices in JavaScript

Understanding the Reasons for Applications to Make Decisions

Deciding When to Use the if Statement

Creating a simple if statement

Deciding what else to do

Nesting if statements

Switching Between Choices

Creating a basic switch

Using the default option

Chapter 9: Making the Rounds with Loops

Discovering Loops

Learning why loops are useful

Running from the dangers of loops

Creating for Loops

Making your first for loop

Using the break statement

Using the continue statement

Creating while Loops

Using the basic while loop

Using the do...while loop

Examining Objects Using for/in

Nesting Loops

Chapter 10: Performing Error Handling

Understanding Errors

Defining an error source

Defining the error types

Catching Errors

Using the try...catch block

Understanding the Error object

Throwing Errors

Part IV: Interacting with Users and HTML

Chapter 11: Understanding the Document Object Model

Introducing the Document Object Model (DOM)

Accessing Individual HTML Elements

Modifying the output stream

Affecting HTML content

Changing attributes

Considering Events

Working with Styles

Working with Nodes

Creating new elements

Removing existing elements

Chapter 12: Handling Events

Introducing Events

Performing basic event tasks

Working with attributes

Clicking to Create an Event

Understanding the objects at your disposal

Passing parameters to the event handler function

Pressing a Key

Creating Custom Events

Working with the CustomEvent object

Creating the custom event code

Chapter 13: Connecting with Style: JavaScript and CSS

Changing HTML Elements

Working with HTML tags

Working with heading styles

Working with IDs

Building Dynamic HTML Elements

Animating and Positioning HTML Elements

Creating JavaScript-Based Menus

Designing the HTML

Defining the styles

Creating the JavaScript functions

Chapter 14: Enhancing HTML5 Forms with JavaScript

Revisiting the HTML5 Form Features

Working with the new elements

Working with the new controls

Understanding the new attributes

Accessing Form Elements

Validating Forms

Chapter 15: Interacting with Windows

Working with Windows

Determining window characteristics

Changing window attributes

Accessing the Screen

Finding a Location

Determining the current location

Changing the location

Searching History

Working with Navigator

Creating a Pop-Up

Controlling line breaks

Creating a modal dialog box

Using Cookies

Setting a cookie

Getting a cookie

Checking a cookie

Part V: Extending JavaScript Further

Chapter 16: Working with XML in JavaScript

Introducing XML

Gaining an overview of XML

Understanding elements

Understanding attributes

Working through a basic file

Displaying XML Using XSLT

Validating XML

Understanding the concept of well-formed

Discovering XSD

Loading XML with JavaScript

Chapter 17: Cleaning Up the Web with AJAX

Introducing AJAX

Learning the benefits of AJAX

Understanding how AJAX works

Deciphering the XMLHttpRequest object

Performing AJAX Tasks Using JavaScript

Making AJAX Easier with jQuery

Chapter 18: Making JavaScript Easier with jQuery

Getting to Know the jQuery Library

Loading jQuery from Google CDN

Doing Things Easier with jQuery

Gathering elements with selectors

Working with programmatically generated elements

Revisiting arrays with jQuery

Interrogating an array

Handling Events with jQuery

Understanding the event functionality

Working with events

Chapter 19: Using jQuery to Handle AJAX

Understanding the AJAX Object in jQuery

Considering the global AJAX event handlers

Working with the helper functions

Understanding the low-level interface

Using shorthand methods

Discovering Server-Side Programming

Adding PHP support to your Apache server

Testing your PHP configuration

Creating the PHP script

Retrieving Results from AJAX Calls

Working with standard output

Investigating the benefits of JSON

Chapter 20: Animating the Web

Getting to Know jQuery UI

Considering interactions

Understanding the widgets

Defining the effects

Canvasing Your Web Page

Understanding what the canvas does

Creating a simple Google API application

Part VI: The Part of Tens

Chapter 21: Ten Incredible HTML5 and JavaScript Examples

Creating an XML-Based Menu

Creating an XML-Based Tabbed Interface

Displaying a Slideshow

Employing Custom Spinners

Working with Timepickers

Creating a Standardized Look with CSS

Displaying a Category and Detail Data View

Selecting Data Using a Calendar

Developing a User-Configurable Interface

Devising a Simple Survey Form

Chapter 22: Ten Thoughts About the Future of Web Development

Automating More Tasks

Developing Applications That Run Anywhere

Using Standards for Every Application

Creating a Desktop Environment with a Browser

Using a Centralized Data Store

Creating Mobile-friendly Applications

Developing Accessible Applications

Building New Application Types

Thinking More About Users

Expecting Unexpected Connections

Cheat Sheet

Introduction

Have you people-watched lately? If not, you really should. People-watching is both fun and educational. At one time, you wouldn’t see people using computers wherever they went because computers were large, cumbersome devices that no one wanted to take out of the office. Today, you have a hard time finding people who aren’t using a computing device of some sort to perform some task. Developers have an incredible opportunity today to affect people in every walk of life and in any situation imaginable because people carry their computing devices with them far and wide to hold every bit of information they find valuable.

The tools that developers use to create applications must change to reflect this new reality, and HTML5 and JavaScript are the most appropriate tools to accomplish the goals developers have today. You can use the combination of HTML5 and JavaScript to create applications that run on any device using just about any new browser. HTML5 Programming with JavaScript For Dummies is your gateway to an incredible new future of development where you aren’t limited to a specific platform or some vendor’s concept of what tools you should use to create applications. This book helps you gain the skills required to create the new sorts of applications that developers have always wanted to write.

About This Book

HTML5 Programming with JavaScript For Dummies is about possibilities. It focuses on getting started writing code quickly and efficiently. You see lots of coding examples, all of which demonstrate principles you need for writing applications that people want to use today. The latter part of the book actually shows some application programming techniques you can use directly in production applications. The method used to accomplish this goal is the same method that all smart developers use today — borrowing code from someone else. This book emphasizes the use of templates and libraries to make your coding experience fun and simple rather than cumbersome and boring, as it might have been in the past.

Don’t worry about becoming immediately lost in detail. Like every For Dummies book, this one takes things slowly, and all the examples are explained thoroughly so that you know precisely how they work. You’ll find that you advance quickly because this book takes advantage of the best possible techniques that knowledgeable developers use to make their lives simple. Working with libraries such as jQuery greatly decreases the work you have to do while simultaneously making the application you create look professional, and best of all, work everywhere on every device.

Of course, every developer needs to be aware of at least the basics, and the introductory chapters of the book do just that — they tell you how things work under the cover. Sometimes you need this information in order to make the best use possible of all those third-party offerings. However, as the text often states, there’s no need to reinvent the wheel. Your JavaScript applications will look professional because you’re using professionally written code to develop them. This book shows you how to create some truly impressive results in an incredibly short timeframe.

What You Don’t Have to Read

Most of the chapters contain some advanced material that will interest only some readers. When you see one of these specialized topics, feel free to skip it. Most of this advanced material appears in sidebars. The sidebar title always indicates the nature of the advanced material.

You can also skip any material marked with a Technical Stuff icon. This material is helpful, but you don’t have to know it to work with HTML or JavaScript. I include this material because I find it helpful in my programming efforts and believe that you will, too.

Foolish Assumptions

You might find it difficult to believe that I’ve assumed anything about you — after all, I haven’t even met you yet! Although most assumptions are indeed foolish, I made these assumptions to provide a starting point for the book.

It’s important that you’re familiar with the platform and browser you want to use because the book doesn’t provide any handholding in this regard. To focus on HTML5 and JavaScript as fully as possible, the book covers browsers marginally and platform requirements not at all. You really do need to know how to install applications, use the browser, and generally work with your chosen platform before you begin working with this book.

Knowing a little about HTML is helpful but not essential. Any experience you have with programming will be helpful as well. The book doesn’t assume you have any knowledge of JavaScript.

Conventions Used in This Book

This book uses special typeface to emphasize some information. For example, entries that you need to type appear in bold. All code, Website URLs, and onscreen messages appear in monofont type. When I define a new word, you see that word in italics.

Because you use multiple applications when you're working with JavaScript, I always point out when to move from one application to the next. However, the testers for this book tried out the code with the Internet Explorer, Firefox, and Chrome browsers on the Macintosh, Linux, and Windows platforms. One tester also checked at least some of the code using a Windows 8 phone. In most cases, you shouldn't experience any problem working with your application unless specifically noted in the application description. Please let me know at John@JohnMuellerBooks.com if you ever experience a problem with one of the examples.

How This Book Is Organized

This book contains several parts. Each part demonstrates a particular JavaScript concept. In each chapter, I discuss a particular topic and include example programs that you can use to discover more about JavaScript on your own. It isn't necessary to read the book cover-to-cover — you can peruse the topics you find most interesting as you would with any reference book. However, you'll get more from the book if you do read it cover-to-cover. You can find the source code for this book on the Dummies.com Website at http://www.dummies.com/go/html5programmingwithjavascript.

Part I: Understanding the Basics of JavaScript

This part of the book helps you gain a perspective of what the language can do for you as a developer and then shows you some simple examples of how the language works. Chapter 1 exposes you to JavaScript by presenting simple examples — you aren’t expected to fully understand how they work, but you do gain an understanding of what JavaScript is like and how it could potentially help you create amazing browser-based applications. Chapter 2 spends time exploring some common tools you should consider using when writing your JavaScript applications. Chapter 3 begins exploring how HTML5 and JavaScript work together to create a useful programming environment.

Part II: Speaking the JavaScript Language

Before you can do anything with JavaScript, you need to know how to speak the language. Chapters 4, 5, and 6 introduce you to the essential elements of the JavaScript language in the form of variables and objects. Everything in JavaScript is an object. Before you can do anything else, you need to understand these objects and discover how to use them.

Part III: Meeting JavaScript’s Control Structures

Complex applications rely on control structures to organize tasks, optionally perform them, and perform them repetitively. Chapter 7 focuses on functions, which are the JavaScript method of organizing tasks in easily understood and reusable pieces. Chapter 8 tells you how to make decisions using JavaScript code. Finally, Chapter 9 discusses techniques for performing tasks repetitively. Taken together, these three chapters help you create complete and useful JavaScript applications.

This part of the book also discusses errors. Errors can happen in every application, even when that application has no errors in it. A user can enter incorrect data or an environmental factor can cause various sorts of data degradation and loss. Many errors are completely out of your hands. However, recovering from these errors is completely within your grasp, which is the topic of Chapter 10.

Part IV: Interacting with Users and HTML

This part of the book focuses on user interactions. The basis of interaction in JavaScript is the Document Object Model (DOM), which is the focus of Chapter 11. To react to user actions and external activity, your application must handle events. Chapter 12 describes all sorts of events and how you can handle them within your application.

Creating a good presentation also helps you motivate users to interact with your application. Chapters 13, 14, and 15 discuss various kinds of presentation techniques you use to create successful applications with JavaScript.

Part V: Extending JavaScript Further

JavaScript is an astounding language that lets you interact with users in new ways that reflect the modern reality of computing device usage. This part of the book exposes you to just a subset of the truly amazing things you can do to extend JavaScript to help you create robust applications. Chapter 16 focuses on XML, which is a technique you can use to store data of all sorts without much effort at all.

One of the techniques you can use to make users more receptive to your applications is to make the application faster and more efficient. That’s the purpose of Asynchronous JavaScript and XML (AJAX). It helps you create efficient applications as described in Chapter 17.

The most incredible feature of JavaScript is the support it enjoys from third party developers. This book can only introduce you to one such library (and even this one library can help you create new applications quickly). Chapters 18, 19, and 20 introduce you to a third-party library named jQuery, which can help you add interactions and special effects to your applications. This library also provides access to a number of interesting widgets that you can use to give your applications a professional appearance.

Part VI: The Part of Tens

This part of the book contains two top ten lists of things related to JavaScript that you’ll find handy as you work your way through your first applications. Chapter 21 contains some parting coding examples and ideas on how to make your coding environment more fun, efficient, and productive. Chapter 22 is all about the future. It discusses some ideas of where application development will go in the future.

The companion Website

This book contains a lot of code, and you might not want to type it. In fact, it's probably better if you don't type this code manually. Fortunately, you can find the source code for this book on the Dummies.com Website at http://www.dummies.com/go/html5programmingwithjavascript. The source code is organized by chapter, and I always tell you about the example files in the text. The best way to work with a chapter is to download all the source code for it at one time.

Icons Used in This Book

As you read this book, you’ll see icons in the margins that indicate material of interest (or not, as the case may be). This section briefly describes each icon in this book.

Tips are nice because they help you save time or perform some task without a lot of extra work. The tips in this book are timesaving techniques or pointers to resources that you should try to get the maximum benefit from JavaScript or HTML5.

I don’t want to sound like an angry parent or some kind of maniac, but you should avoid doing anything marked with a Warning icon. Otherwise, you could find that your program melts down and takes your data with it.

Whenever you see this icon, think advanced tip or technique. You might find these tidbits of useful information just too boring for words, or they could contain the solution you need to get a program running. Skip these bits of information whenever you like.

If you don’t get anything else out of a particular chapter or section, remember the material marked by this icon. This text usually contains an essential process or a bit of information that you must know to write JavaScript programs successfully.

There are times when a topic is so immense that you really need additional information about it. Often, the topic covered is outside the scope of this book, and discussing it in the book would become distracting and would divert attention away from programming HTML5 with JavaScript. Paragraphs marked with this icon contain links to online sources you can use to discover more about a topic that will interest you, but you don’t need to know about it in order to understand the code in the book.

Where to Go from Here

It’s time to start your HTML5 programming with JavaScript adventure! If you’re a complete JavaScript novice, you should start with Chapter 1 and progress through the book at a pace that allows you to absorb as much of the material as possible. If you’re in an absolute rush to get going with JavaScript as quickly as possible, you could possibly skip to Chapter 2 with the understanding that you may find some topics a bit confusing later.

Everyone should read Chapter 2 because it contains some setup information you need to use the examples effectively. The book assumes that you have some type of Web server set up to run examples that require it. Because Apache runs on all of the target platforms for this book (Linux, Windows, and Mac OS X), I recommend you install this particular Web server. In addition, Apache is the most popular server on the Internet, so you’ll definitely encounter it at some point.

Advanced readers, those who already have a basic understanding of JavaScript, can save time by reading Chapter 2 and then moving directly to Chapter 11. If you’re really in a rush to get to the best material in the book, you can always try to start with Chapter 17 and then go back to earlier chapters as necessary when you have questions. However, it’s important that you understand how each example works before moving to the next one. Every example has important lessons for you, and you could miss vital content if you start skipping too much information.

Occasionally, Wiley's technology books are updated. If this book has technical updates, they'll be posted at http://www.dummies.com/go/html5programmingwithjavascriptupdates.

Part I

Understanding the Basics of JavaScript

Visit http://www.dummies.com for more great For Dummies content online.

In this part . . .

Discover why JavaScript is such an important addition to your programming toolbox.

Create a simple example that demonstrates the basics of using JavaScript.

Obtain a basic list of tools you can use to make writing JavaScript applications easier.

Uncover the hidden developer features found in most browsers.

Install Apache on your system to use as a test site.

Understand basic JavaScript programming language terminology.

Determine when visitors have JavaScript turned off in their browsers.

Chapter 1

HTML, Say Hello to JavaScript

In This Chapter

arrow Understanding what JavaScript is

arrow Understanding how JavaScript fits in with HTML5

JavaScript is a text-based scripting language that’s interpreted by a client system to perform tasks in various settings. The most common setting is within browsers. A developer wants to do something special, such as accept input from a form, and JavaScript makes it possible.

JavaScript appears in many other places. For example, Windows has long allowed the use of JavaScript to create applications, and now it has an even bigger role with Windows 8. (See http://msdn.microsoft.com/library/windows/apps/br211385.aspx for details.) Special versions of JavaScript also support application development on the Macintosh. (See www.latenightsw.com/freeware/JavaScriptOSA as an example.) In fact, you can even run Linux in a browser by using a JavaScript emulator. (See www.webmonkey.com/2011/05/yes-virginia-that-is-linux-running-on-javascript for details.) The point is that JavaScript is a language that appears in all sorts of places on many different operating systems. When you discover JavaScript, you open an exciting new world of programming that works on myriad platforms — a dream that developers have had for a very long time.

This book doesn’t explore all of the possible environments in which you can use JavaScript. I doubt very much that you could examine the topic in any detail with an entire shelf of books. What you’ll encounter is how JavaScript is used with HTML5, the newest version of the HyperText Markup Language (HTML). HTML5 and JavaScript are made for each other. By combining these two languages, you create a robust environment for Web applications. Modern Web applications can perform an amazing array of tasks — everything from word processing to database entry. The use of HTML5 and JavaScript together makes it possible for anyone or any organization to move applications from the desktop to the cloud (a special location on the Internet used to store applications and data), where any device capable of running JavaScript can access and use them. In short, combining HTML5 with JavaScript can free users from using a specific device to interact with any application you can imagine.

Of course, any book on a programming language must begin with some basics and present some ground rules, which is precisely what you find in this chapter. You discover a little more about what JavaScript is and how it can help you create interesting applications. You’ll also begin creating some basic JavaScript applications in this chapter. They won’t do too much at first; you’ll gain a sense of what JavaScript can do after you’ve worked with it some more.

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!