Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Taking_Your_Talent_to_the_Web.pdf
Скачиваний:
5
Добавлен:
11.05.2015
Размер:
9.91 Mб
Скачать

286 HOW: The Joy of JavaScript: What Is This Thing Called JavaScript?

WHAT IS THIS THING CALLED JAVASCRIPT?

JavaScript is a programming language designed specifically to work in web browsers. Its purpose is to bring interactivity to sites. Though JavaScript is powerful and complex, it is relatively easy to learn—at least it is easier to learn than many other programming languages.

Even before JavaScript, sites could be somewhat interactive. After all, clicking a hyperlink loads a new page. The nonlinear nature of hypertext allows the reader to decide where to go next—to structure her own voyage through the site (and, indeed, through the Web).

That is an interactive process and a rather profound one. But it is not a terribly sophisticated form of interactivity. JavaScript puts the top hat and tails on web interactivity.

The Web Before JavaScript

Before JavaScript, programming languages such as Perl were used to facilitate interactive processes, for example typing text into a form and clicking a button, thereby sending requested information to the site’s owners. Perl is still often used for this purpose on a great many sites.

But Perl is a server-side scripting language. That is, when a visitor clicks the Send button, the web server itself must process the script. If the server goes down or malfunctions, nothing will happen. Likewise, a web page not connected to a web server—say, a web page on your hard drive—would not be able to process such a script except in special circumstances (permanent Net connection, full URL specified in the <FORM ACTION>, burning of incense, wearing of magic ring).

While the script is being processed, the web server is momentarily tied up— just as your Mac or PC gets tied up when you apply motion blur to an image in Photoshop. Imagine constantly applying motion blur while receiving and sending email, and you begin to see what web servers were up against. (This is, of course a crude picture, and if you like it, we have other crude pictures available at a nominal price.)

Taking Your Talent to the Web

287

As the Web’s population grew, more and more users were clicking more and more Send buttons. More and more web servers were thus spending more and more time processing scripts in between serving web pages. Meanwhile, the user’s browser sat there, doing nothing. A tool was needed to transfer the process from the server to the client (the user’s computer). Enter JavaScript.

JavaScript, Yesterday and Today

In 1995, the company formerly known as Netscape Communications Corporation introduced a client-side programming language designed to transfer the burden of interactive processing from the web server to the end-user’s browser. Unlike other programming languages (such as Java or C), this new language was built into the browser. It even understood HTML.

Netscape called its new client-side scripting language LiveScript, but soon changed the name to JavaScript to capitalize on the growing excitement about Sun’s Java language. To this day, as a result of their similar names, many beginning web designers (and users) confuse Java with JavaScript. The relationship between the two is mainly one of marketing.

Netscape quickly promised to release JavaScript as a web standard so that other browsers could use it too. But for competitive reasons, the company initially held back on its pledge. Old browsers like Mosaic could not use JavaScript at all. Neither could Microsoft’s newly unveiled Internet Explorer.

To offset Netscape’s advantage, Microsoft’s browser engineers developed a JavaScript-like language called JScript. Web design quickly became a circle of Hell, as serious developers were forced to work with these similar but incompatible technologies. Freelancers and small firms, lacking deep resources, often chose to support only one technology. That one was nearly always JavaScript, especially in the beginning when Internet Explorer enjoyed only a tiny share of the market. Thus JavaScript functioned as a sort of standard before it really was one.

288 HOW: The Joy of JavaScript: JavaScript, Unhh! What Is It Good For?

Around the Millennium, JavaScript finally became an official web standard, available to all. As reported in Chapter 2, “Designing for the Medium,” the European Computer Manufacturers Association (ECMA) supervised the standardization of JavaScript, renaming it in the process. The universal web scripting language is now officially known as ECMAScript or ECMA-262, though no one we know calls it anything but JavaScript. (Our Scandanavian friends pronounce it “Ya-va-script,” which we find incredibly endearing. That has nothing to do with any of this, but it does lend this chapter a certain international flair.)

The point is that JavaScript/ECMAScript is a standard that works in all current browsers, though there are still some subtle incompatibilities being worked out between the latest versions of Netscape, Internet Explorer, Opera, and other browsers. Meanwhile, the DOM has been standardized by W3C. Prior to that, JavaScript had its own ever-changing DOM in Netscape, and Microsoft had its own DOM as well.

As all browsers finalize complete support for these standards, web designers and developers are being empowered to create sophisticated interactivity that works for everyone. Conversely, as browsers lag in their DOM and ECMAScript support, web designers and developers are stuck programming alternate versions of every site function, often going so far as to develop alternate versions of the sites themselves.

Now that we’ve concluded our mini-history lesson and you’ve finished your nap, let’s move on to the good stuff.

JAVASCRIPT, UNHH!

WHAT IS IT GOOD FOR?

Absolutely lots of things. Through this web-friendly programming language, designers and developers can:

Replace cryptic status bar URLs (“http://www.doglovers.com/ poodles.html”) with text messages (“Learn about poodles”), a somewhat controversial practice, for reasons we’ll discuss in a moment.

Create the ever-popular image rollover effect discussed in Chapter 9, “Visual Tools.”

Taking Your Talent to the Web

289

Compensate for browser incompatibilities.

Open new, precisely sized “pop-up” windows, with or without various bits of browser chrome.

Test for the presence or absence of Flash, QuickTime, or other plugins (more about plug-ins in Chapter 12, “Beyond Text/Pictures”).

Rotate content and images depending on the time of day, the number of times the user has viewed a certain page, or simply at random.

Enable the client to inject 50 links on a page without cluttering that page at all.

Provide alternative means of navigating the site.

“Remember” that a user has visited the site before, sparing her the pain of reentering personal data or passwords. This is accomplished by means of cookies (Netscape terminology for little bits of text that reside on the end-user’s hard drive and can be recognized by JavaScript on subsequent visits to the site).

Cause images or text boxes to scroll horizontally or vertically, another controversial and often annoying practice.

Verify the credibility of email addresses entered on “customer feedback” forms. JavaScript won’t tell you if a person is using her email address or someone else’s, but it will tell you if the address is well formed or not. (If malformed, it is probably a nonworking address.)

Control complex frames—of less importance than it used to be, as frames are gradually being phased out. Similarly, JavaScript can protect sites from a third party’s poorly crafted frames.

Create nested navigational menus that reveal secondary and tertiary levels in response to cursor movements—a wonderful idea because it enables visitors to navigate directly to the information they seek, but problematic because not all browsers fully support a standard means of doing this.

…and much more.

Add the W3C DOM to what JavaScript does already and you can change that phrase to “much, much, much more.”

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]