Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Ajax and JavaScript

Essential JavaScript

  • Print
  • + Share This
Steven Holzner provides the necessary skills and techniques for his guided tour of JavaScript. He begins with a history of the language.
This chapter is from the book

This chapter is from the book

Welcome to the world of JavaScript. This book is your guided tour to that world, so have no worries—you've come to the right place. That world is large and expanding in unpredictable ways every minute, but we're going to become familiar with the lay of the land in detail here. And there's a lot of territory to cover, because JavaScript is getting into the most amazing places, and in the most amazing ways, these days.

You'll see JavaScript just about everywhere you go on the Internet; it's the language of the web. JavaScript is a powerful, compact scripting language that gets embedded right in web pages, and brings them to life. In this book, we're going to see all of JavaScript at work, from the basics to the most advanced; from mouse rollovers to dynamic HTML; from status bar manipulations to opening new browser windows on command; from working with the mouse and keyboard to handling HTML controls such as buttons, check boxes, and drop-down menus; from working with XML and XSLT to working with .NET; from using style sheets to setting cookies. JavaScript supports a dazzling variety of options for your web pages, and it's all coming up in this book. We're going to see it all at work—and everything is going to be illustrated with at least one example.

This first chapter is our foundation chapter, where we'll get the necessary skills and techniques down to begin our guided tour of the language. To put things into perspective, a good place to start is with a history of JavaScript.

JavaScript Through the Ages

JavaScript is a unique language—despite its name, which has caused a great deal of confusion, it is not directly related to Java. When JavaScript was first introduced, Java was dominant, and it must have seemed like a good idea to tie JavaScript to Java.

Things have shifted dramatically, however; JavaScript has become far more popular than Java for web use. Whereas Java has gone on to become a relatively complex, involved language, JavaScript has remained remarkably the same over the years—a lightweight, fast, and powerful language that is, quite simply, the programmer's favorite—it's by far the favorite language of the web.

It all began in Netscape Communications Corporation in 1995, when a developer named Brendan Eich, who they had just lured away from MicroUnity Systems Engineering, was given a new project to work on. The original idea was to make programming more accessible than it was becoming in Java, and to somehow make the Netscape Navigator's new Java support more open to non-Java programmers. Eich took a look around and decided that what was needed was a scripting language that was fast and easy to use. Unlike Java, which first has to be compiled into "bytecodes," JavaScript was to be embedded right in the web page itself, and downloaded with that page. And as Java itself has become more and more inaccessible, JavaScript's star just continues to rise.

Brendan Eich called his creation LiveScript, but it was renamed JavaScript. (In fact, many people consider the name change a big mistake.) Although developed by Netscape, Sun had the trademarks on Java, and the name JavaScript is actually a trademark of Sun Microsystems, Inc. The new language was announced in a Netscape and Sun joint press conference on December 4, 1995. They originally positioned it as a "complement" to Java and HTML, rather than a simple support language for Java.

However, JavaScript rapidly slipped out of Java's shadow. Programmers took to it at once, and although there were problems with bugs and later with security, JavaScript was a success. It became clear that, as Eich had realized, programming a web page in the browser made all kinds of things possible that couldn't be done if you needed to reload the page from the server. (In fact, the most popular, original use for JavaScript was to swap images when the mouse rolled over them, and that continues to be a very popular thing to do.)

Web programmers found JavaScript programming fun. To some extent, that meant that many programmers didn't take it seriously. Java was a serious language, and as time has gone on, it has become much more serious—serious, unfortunately, to the point of inaccessibility for much of its intended audience. JavaScript, originally a guilty pleasure for many, started to be accepted by thousands of programmers. As browsers became more powerful and could run scripts faster (eliminating screen flicker and other problems), programmers discovered that JavaScript was indeed a viable development tool. And all it took was a few lines of code to get things working, making buttons work, and setting cookies.

As JavaScript started making waves, Netscape's chief competitor, Microsoft, could no longer ignore it. It's interesting to speculate how things would have gone if JavaScript had been a public standard in those days, developed, say, by a neutral third party. But JavaScript was a product of Microsoft's direct competitor in this area, Netscape, so Microsoft decided to create its own version of JavaScript, which it called JScript.

The first official version of JScript was released on July 16, 1996, in Internet Explorer 3.0. From then on, there was both JavaScript and JScript, and the resulting split personality for the language between Netscape and Microsoft has had repercussions that echo down to today. The original JScript was much like a reduced version of JavaScript 1.1, however, and the differences confused programmers. At that point, Microsoft had created its own scripting language, VBScript, based on its popular Visual Basic language, and was hoping to successfully rival JavaScript. JavaScript was triumphant, however, and VBScript is a very distant second these days.

To bring their JScript support up to full JavaScript 1.1, Microsoft later released a second version of Internet Explorer 3.0. So started the cross-browser and cross-browser version problems that have made life harder for the JavaScript programmer ever since. Programmers started to find that although JScript looked just like JavaScript, some scripts would run in Netscape and not in Internet Explorer, and vice versa.

Hoping to stave off chaos, Netscape and Sun looked for a third party to standardize JavaScript, and turned to the European Computer Manufacturers Association (ECMA, http://www.ecma.ch). Unfortunately for programmers, that standardization, which began in November 1996, has been a long time coming, and even longer to be adopted. You might expect the standardized language to be called JavaScript, or possibly JScript, but it's not called either—it's ECMAScript.

So now we actually have three standards: JavaScript, JScript, and ECMAScript. Although ECMAScript has brought some measure of coherence to the picture, both browsers also have gone their own ways. The result is that the Internet Explorer often has two ways of letting you do things (the JScript way and the ECMAScript way), and the Netscape Navigator has two ways of letting you do things (the JavaScript way and the ECMAScript way). A big problem was that ECMAScript standardized the core JavaScript language itself, but not all the resources available in browsers that you work with—and those resources make up a much larger field than the JavaScript language does by itself.

These browser resources are made up of browser objects (discussed later in this chapter). Each browser had its own set of objects, such as the document object, which enables you to work with the web page, and the window object, which enables you to work with the browser itself. A great deal of JavaScript programming is all about working with these objects; and if they're entirely different in the two browsers, it's easy to see that the same JavaScript-enabled web pages are going to have problems in one or the other browser—so much so that people today are still coding different scripts in the same web page for the two browsers.

The chaos raised by different sets of browser objects threatened both JavaScript and JScript, so Microsoft, Netscape, and many other corporations went to the World Wide Web Consortium (W3C, http://www.w3.org) to bring some coherence to the objects browsers would make accessible to JavaScript. The original browser object set, before W3C started its work, became known as the Document Object Model (DOM) "level 0." The DOM level 1, which became an official W3C specification on October 1, 1998, standardized many aspects of the objects found in browsers, but left out many others. DOM level 2 was made a specification on November 13, 2000, and fills in many more details. However, both DOM models omit any discussion of many of the popular objects available in both browsers, which means that much of what we're going to do is try to reconcile programming issues between those browsers. Work on the DOM level 3, now in the working draft stage, has started.

The result of all this is that today, JavaScript is an amazingly powerful tool for web programmers, but it's still plagued by cross-browser issues. The current version of JavaScript in the Netscape Navigator is 1.5 (with 2.0 on the way), and the current JScript version is 5.6. With each new version, more power from other languages has been packed into JavaScript, borrowing from languages such as C++, Java, Perl, and even TCL.

Although the W3C DOM has brought some calm to the war between the browsers, it's also true that market share has dramatically shifted to Microsoft. It would be a shame if the only way JavaScript compatibility could finally be ensured were by the demise of one of the competitors, but it appears that might be happening. Nonetheless, we're going to work with both browsers in this book—in fact, if there's an incompatibility, I'll indicate not only which browser something applies to, but which versions of that browser. That's the only way to do it.

We'll take a look at cross-browser programming in more detail both in this chapter and in Chapter 4, "Handling the Browser Environment." The very fact that incompatibility issues exist indicates that both Netscape and Microsoft consider JavaScript very important. And despite the cross-browser problems, JavaScript continues to flourish. So, in just what kind of ways do people use JavaScript these days? I'll take an overview of the JavaScript world now.

  • + Share This
  • 🔖 Save To Your Account