JavaScript Editor JavaScript Validator     JavaScript Editor 

Team LiB
Previous Section Next Section

Introduction to JavaScript

In this section, we're going to take a brief look at what JavaScript is, where it came from, how it works, and what sorts of useful things we can do with it.

What Is JavaScript?

Having bought this book you are probably already well aware that JavaScript is some sort of computer language, but what is a computer language? Put simply, a computer language is a series of instructions that instruct the computer to do something. That something can be a wide variety of things, including displaying text, moving an image, or asking the user for information. Normally the instructions, or what is termed code, are processed from the top line downward. Processed simply means that the computer looks at the code we've written, works out what action we want taken, and then takes that action. The actual act of processing the code is called running or executing it.

Using natural English, let's see what instructions, or code, we might write to make a cup of coffee.

  1. Put coffee in cup.

  2. Fill kettle with water.

  3. Put kettle on to boil.

  4. Has the kettle boiled? If so, then pour water into cup; otherwise, continue to wait.

  5. Drink coffee.

We'd start running this code from the first line (instruction 1), and then continue to the next (instruction 2), then the next, and so on until we came to the end. This is pretty much how most computer languages work, JavaScript included. However, there are occasions when we might change the flow of execution, or even skip over some code, but we'll see more of this in Chapter 3.

JavaScript is an interpreted language, rather than a compiled language. What do we mean by the terms interpreted and compiled?

Well, to let you in on a secret, your computer doesn't really understand JavaScript at all. It needs something to interpret the JavaScript code and convert it into something that it understands; hence it is an interpreted language. Computers only understand machine code, which is essentially a string of binary numbers (that is, a string of zeros and ones). As the browser goes through the JavaScript, it passes it to a special program called an interpreter, which converts the JavaScript to the machine code your computer understands. It's a bit like having a translator to translate English into Spanish, for example. The important point to note is that the conversion of the JavaScript happens at the time the code is run; it has to be repeated every time the code is run. JavaScript is not the only interpreted language; there are others, including VBScript.

The alternative compiled language is one where the program code is converted to machine code before it's actually run, and this conversion only has to be done once. The programmer uses a compiler to convert the code that he wrote to machine code, and it is this machine code that is run by the program's user. Compiled languages include Visual Basic and C++. Using a real-world analogy, it's like having someone translate our English document into Spanish. Unless we change the document, we can use it without retranslation as much as we like.

Perhaps this is a good point to dispel a widespread myth: JavaScript is not the script version of the Java language. In fact, although they share the same name, that's virtually all they do share. Particularly good news is that JavaScript is much, much easier to learn and use than Java. In fact, languages like JavaScript are the easiest of all languages to learn but are still surprisingly powerful.

JavaScript and the Web

For most of this book we'll be looking at JavaScript code that runs inside a web page loaded into a browser. All we need to create these web pages is a text editor, for example, Windows NotePad, and a web browser, such as Netscape Navigator or Internet Explorer, with which we can view our pages. These browsers come equipped with JavaScript interpreters.

In fact, the JavaScript language first became available in the web browser Netscape Navigator 2. Initially, it was called LiveScript. However, because Java was the hot technology of the time, Netscape decided that JavaScript sounded more exciting. Once JavaScript really took off, Microsoft decided to add their own brand of JavaScript to Internet Explorer, which they named JScript. Since then, both Netscape and Microsoft have released improved versions and included them in their latest browsers. Although these different brands and versions of JavaScript have much in common, there are enough differences to cause problems if we're not careful. Initially we'll be creating code that'll work with Netscape and Microsoft version 4 and later browsers. Later chapters of the book look at features available only to Netscape 6+ and IE 5.5+. We'll look into the problems with different browsers and versions of JavaScript later in this chapter, and see how we deal with them.

The majority of the web pages containing JavaScript that we will create in this book can be stored on your hard drive and loaded directly into your browser from the hard drive itself, just as you'd load any normal file (such as a text file). However, this is not how web pages are loaded when we browse websites on the Internet. The Internet is really just one great big network connecting computers together. Websites are a special service provided by particular computers on the Internet; the computers providing this service are known as web servers.

Basically the job of a web server is to hold lots of web pages on its hard drive. When a browser, usually on a different computer, requests a web page that is contained on that web server, the web server loads it from its own hard drive and then passes the page back to the requesting computer via a special communications protocol called HyperText Transfer Protocol (HTTP). The computer running the web browser that makes the request is known as the client. Think of the client/server relationship as a bit like a customer/shopkeeper relationship. The customer goes into a shop and says, "Give me one of those." The shopkeeper serves the customer by reaching for the item requested and passing it back to the customer. In a web situation, the client machine running the web browser is like the customer and the web server getting the page requested is like the shopkeeper.

When we type an address into the web browser, how does it know which web server to get the page from? Well, just as shops have addresses, say, 45 Central Avenue, SomeTownsville, so do web servers. Web servers don't have street names; instead they have Internet Protocol (IP) addresses, which uniquely identify them on the Internet. These consist of four sets of numbers, separated by dots; for example,

If you've ever surfed the net, you're probably wondering what on earth I'm talking about. Surely web servers have nice names, not IP addresses? In fact, the name is the "friendly" name for the actual IP address; it's a whole lot easier for us humans to remember. On the Internet, the friendly name is converted to the actual IP address by computers called domain name servers, something your Internet service provider will have set up for you.

Toward the end of the book, we'll go through the process of how to set up our own web server in a step-by-step guide. We'll see that web servers are not just dumb machines that pass pages back to clients, but in fact they can do a bit of processing themselves using JavaScript. We'll be looking at this later in the book as well.

Why Choose JavaScript?

JavaScript is not the only scripting language; there are others such as VBScript and Perl. So why choose JavaScript over the others?

The main reason for choosing JavaScript is its widespread use and availability. Both of the most commonly used browsers, Internet Explorer and Netscape Navigator, support JavaScript, as do some of the less commonly used browsers. So, basically we can assume that most people browsing our website will have a version of JavaScript installed, though it is possible to use a browser's options to disable it.

Of the other scripting languages we mentioned, VBScript, which can be used for the same purposes as JavaScript, is only supported by Internet Explorer running on the Windows operating system, and Perl is not used at all in web browsers.

JavaScript is also very versatile and not just limited to use within a web page. For example, it can be used in Windows to automate computer administration tasks and inside Adobe Acrobat.pdf files to control the display of the page just as in web pages, although this is a more limited version of JavaScript. However, the question of which scripting language is the most powerful and useful has no real answer. Pretty much everything that can be accomplished in JavaScript can be done in VBScript, and vice versa.

What Can JavaScript Do for Me?

The most common uses of JavaScript are interacting with users, getting information from them, and validating their actions. For example, say we want to put a drop-down menu on the page so that users can choose where they want to go to on our website. The drop-down menu might be plain old HTML, but it needs JavaScript behind it to actually do something with the user's input. Other examples of using JavaScript for interactions are given by forms, which are used for getting information from the user. Again these may be plain HTML, but we might want to check the validity of the information that the user is entering. For example, if we had a form taking a user's credit card details in preparation for the online purchase of goods, we'd want to make sure they had actually filled in their credit card details before we sent them the goods. We might also want to check that the data being entered is of the correct type, such as a number for their age rather than text.

JavaScript can also be used for various "tricks." One example is switching an image in a page for a different one when the user rolls her mouse over it, something often seen in web page menus. Also, if you've ever seen scrolling messages in the browser's status bar (usually at the bottom of the browser window) or inside the page itself and wondered how they manage that, this is another JavaScript trick that we'll demonstrate later in the book. We'll also see how to create expanding menus that display a list of choices when a user rolls his or her mouse over them, another commonly seen JavaScript-driven trick.

Tricks are OK up to a point, but even more useful are small applications that provide a real service. For example, a mortgage seller's website that has a JavaScript-driven mortgage calculator, or a website about financial planning that includes a calculator that works out your tax bill for you. With a little inventiveness you'll be amazed at what can be achieved.

Team LiB
Previous Section Next Section

JavaScript Editor JavaScript Validator     JavaScript Editor