Section 14.1. Understanding JavaScript

14.1. Understanding JavaScript

The JavaScript language has a long historyit first hit the scene with the Netscape Navigator 2 browser in 1995, and Internet Explorer jumped on the bandwagon by adding JavaScript compatibility to IE version 3. Today, all modern browsers support JavaScript, and it's become wildly popular as a result. However, some justifiably paranoid surfers turn off the JavaScript switch in their browser settings (since malicious developers have, on occasion, used JavaScript-fueled agents to attack computers with pop-up ads and other types of browser annoyances). That means the best rule of thumb is to use JavaScript to improve your page, but make sure your page still works (even if it doesn't look quite as nice) when JavaScript has been disabled.


Note: JavaScript is thoroughly different from the Java language (although the code sometimes looks similar, because they share some syntax rules). Java was developed by Sun Microsystems, and is a full-fledged programming language, every bit as powerfuland complicatedas C++, C#, and Visual Basic.

So what can JavaScript do?

  • JavaScript can dynamically insert some HTML into a Web page, or modify an existing HTML tag. For example, you can show a personalized message, or make a title grow and shrink perpetually (an example of which is shown on Section 14.3.1.1).

  • JavaScript can gather information about the current date, the surfer's browser, and the choices he's made when presented with a form element like a dropdown menu or a radio button. You can display any of this information or use it to make decisions about what page to show next . For example, you could stop surfers from going any further until they type in an email address.

  • JavaScript can react to events that happen in the browser. For example, you can write code that runs when something specific happens (like when a page has finished loading or when a surfer clicks a picture).

It's just as important to understand what JavaScript can't do. JavaScript code is sandboxed , which means the browser locks your page into a carefully controlled place in memory (known as the sandbox) so it can't access anything on the Web surfer's computer. This design, which is necessary to ensure good security, effectively prevents JavaScript from sending orders to the printer, reading or writing (creating or editing) files, running other programs, reformatting your hard drive, and so on. Just about the only thing JavaScript is allowed to do is read and modify the HTML of the current Web page.

14.1.1. Server-Side and Client-Side Programming

In order to understand how JavaScript fits into the Web universe, it's important to understand that there are two types of programming on the Web.

When you surf to a search engine like Google or an e-commerce Web site like Amazon, you're actually connecting to a high- powered piece of software that runs on a Web server. This program is what's known as a server-side application . When you're using one of these Web sites, you send the program some information (like the keywords you want to search for, or the book you want to buy) and the application consults a massive database and spits out some HTML that creates the page you see in your browser.

Server-side applications rule the Web world, because there's virtually nothing they can't do. However, they're insanely difficult to program. Not only do developers need to worry about generating the HTML for the browser, they also need to run all kinds of complex routines and consult giant databasesand they need to do it all in a way that performs just as well when millions of people are clamoring for attention as it does when there's only one person surfing the site. This is hard work, and it's best handled by the poor souls we call programmers.

Client-side applications , on the other hand, use a completely different model. They embed a small, lightweight program inside an ordinary HTML page. When a browser downloads this page, the browser itself can then run the program ( assuming it hasn't been disabled by security settings or compatibility issues). Client-side applications are much less powerfulfor example, they have no reliable way to access the huge databases stored on Web servers, and for safety reasons they're prevented from directly changing most things on your home computer. However, they're much simpler to create. If you've ever played a game of Java checkers in your browser (see Figure 14-1), you've used a client-side program.

Figure 14-1. This game of Java checkers looks like an ordinary Web page, but something very different is taking place behind the scenes. In fact, the checkerboard is actually a complete Java program that's embedded inside the Web page.


14.1.2. Scripting Languages

Even client-side applications can be a challenge for ordinary non-technogeeks. For example, to build the checkers game shown in Figure 14-1, you need to know your way around the highly sophisticated Java programming language. However, a whole class of client-side applications exist that aren't nearly as ambitious. They're called scripts , and their purpose in life is to give the browser a list of instructions (like "scroll that heading from left to right" or "pop up an ad for fudge-flavored toothpicks in a new window").

Scripts are written in a simplified scripting language , and even if you don't know all the ins and outs of a language, you can often copy and paste a cool script from another Web site to get instant gratification. Two examples of scripting languages are JavaScript and VBScript (a scripting language that uses syntax that resembles Visual Basic).

This chapter focuses exclusively on scripts created with JavaScript, the only scripting language that's reliably supported on most browsers.



Creating Web Sites. The Missing Manual
Creating Web Sites: The Missing Manual
ISBN: B0057DA53M
EAN: N/A
Year: 2003
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net