Learn JavaScript With Examples


Chapter 4 - JavaScript

HTML is a simple text markup language, it can't respond to the user, make decisions, or automate repetitive tasks. Interactive tasks such as these require a more sophisticated language: a programming language, or a scripting language. Although many programming languages are complex, scripting languages are generally simple. They have a simple syntax, can perform tasks with a minimum of commands, and are easy to learn. Web scripting languages enable you to combine scripting with HTML to create interactive web pages. JavaScript is one of the most popular and widely used scripting language of the web and is used in billions of Web pages to add functionality, validate forms, communicate with the server, and much more. It is primarily a client-side scripting language for use in Web browsers. Its main focus is to help developers interact with Web pages and the Web browser window itself. Because it is embedded in all modern browsers, it has an extraordinary wide distribution. One of the most powerful features of the language is its flexibility. As a JavaScript programmer, you can make your programs as simple or as complex as you wish them to be. The language also allows several different programming styles. You can write your code in the functional style or in the slightly more complex object-oriented style. It also lets you write relatively complex programs without knowing anything at all about functional or object-oriented programming; you can be productive in this language just by writing simple functions. It allows programmers to accomplish useful tasks with a very small, easy-to-learn subset of the language. JavaScript is almost as easy to learn as HTML, and it can be included directly in HTML documents. Here are the few things you can do with JavaScript:

- Display messages to the user as part of a web page, in the browser's status line, or in alert boxes.

- Validate the contents of a form and make calculations (for example, an order form can automatically display a running total as you enter item quantities).

- Animate images or create images that change when you move the mouse over them.

- Create ad banners that interact with the user, rather than simply displaying a graphic.

- Detect the browser in use or its features and perform advanced functions only on browsers that support them.

- Detect installed plug-ins and notify the user if a plug-in is required.

- Modify all or part of a web page without requiring the user to reload it.

- Display or interact with data retrieved from a remote server.