My Profile

Friday, January 21, 2011

Getting Started with JavaScript


The process of developing a JavaScript application is simple. The following sections introduce the tools you need to create and execute JavaScript scripts and describe the process of combining a script with an HTML page. Finally, you'll learn about the hardware and software platforms that currently support JavaScript.

Required Software and Hardware

Currently, the only requirement in this area (and the only option) is Netscape Navigator, version 2.0b or higher. The available hardware platforms are discussed later in this section. Several other browser manufacturers plan to support JavaScript in the near future, however.

Development Tools

Provided you have a version of Netscape to view your creations on, there are no tools that are specifically required in order to develop a JavaScript script. All you need to create the scripts is a text or HTML editor-probably the same one that you use to develop HTML pages.
So far, none of the dedicated HTML editors have announced support for JavaScript; however, you can still use them to create JavaScript programs. Several HTML editors are included on the CD-ROM accompanying this guide.


Testing a Simple JavaScript Program

A JavaScript program can be simple-even as small as a single statement-or much more complicated. Take a look at the simple example in Listing 1.2.

Listing 1.2. (ALERT.asp) A script that displays a message in the text and in an alert box.
<HTML><HEAD> <TITLE>Another JavaScript Test </TITLE> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> document.write("Hello!"); window.alert("Hello again!"); </SCRIPT> </BODY> </HTML>

This example displays the "Hello!" message as part of the HTML document. In addition, it uses the alert() statement to display a message to the user in a dialog box.
What happens when you load this Web page in Netscape? Let's take a quick look at the inner workings of JavaScript. Once you understand what happens and in what order, you'll find it easy to learn the specifics-such as the JavaScript language itself.

Receiving the Web Page

Let's start with a review of the basics: What happens when you request a Web page from a server? This is an important process to understand. Here are the steps involved:
  1. You enter a URL into your browser, or select a guidemark.
  2. The browser sends an HTTP request for the URL to the appropriate server (known).
  3. The server sends back the contents of the Web page at the URL.
  4. The browser sends additional requests for each of the graphics page.
  5. After receiving enough information about the graphics to devote the correct amount of space to them, the browser displays the page.
  6. The remaining graphics are displayed as they are received.

Processing the Script

When JavaScript is involved, the process is slightly different. After the full HTML document is received, as in step 3 in the last section, it is examined for the <SCRIPT> tag. If a script is included, it is processed; however, the processing depends on the type of script:
  • If the script is included in the header, it is ignored unless a script later calls it.
  • If the script is included directly in the body, its output will be included in the Web page-thus, it can affect the display of the page.
  • If the script is an event handler for a specific part of the page, it will be processed only when the event happens.
All three of these are useful ways of implementing a JavaScript program; you choose one of these methods depending on your needs. In a complicated JavaScript application, you will probably use all three.

Potential Problems with JavaScript

In this section, you learn some of the problems you might encounter when using JavaScript in your Web pages. Because these concerns might affect the security of your source code and data, you should also carefully consider them while developing JavaScript applications.

Security Considerations

As JavaScript has been developed, there have been several concerns about security-most of them legitimate. The Computer Incident Advisory Committee (CIAC), an agency of the Department of Energy that monitors computer security problems, has reported several minor problems with JavaScript.
Because JavaScript is limited in its capabilities, there are no major problems-such as a method for a wayward script to erase your hard drive. All the potential problems involve the owner of the script or Web page being able to access information on your system. Here are the incidents the CIAC has reported:
  • A JavaScript script can read your URL history and report it to the remote site.
  • The browser's disk cache can be read and transmitted.
  • Your e-mail address can be read-and used to send an e-mail without your knowledge.
  • A list of files on your hard disk can be obtained.
  • URLs you visit can be logged by a script and transmitted.
All these problems are specific to Netscape's implementation of JavaScript. The good news is that they have all been fixed by later versions of Netscape. The first two were fixed by version 2.0; the remaining ones are fixed by versions 2.01 and 2.02.
If you have the latest version of Netscape, you should be safe from these problems. However, considering that this many problems have been discovered, there is always a chance there will be more.

Your Source Code Is Accessible

Another problem with JavaScript is that, currently, there is no way to use a JavaScript on your Web page without making the script source available to anyone who wishes to copy it.
This means that you might spend hours working on a script, only to find that people have copied it onto their own Web pages. You may be programming in JavaScript for fun and not mind this, but it might be a serious consideration if you are making a JavaScript for pay or specifically for your own Web page.
Remember that even though your source code is accessible, that doesn't mean it's free to be copied. You legally have a copyright on any script you write.
If someone does copy your scripts, often the best solution is to send that person a friendly note pointing out your copyright and saying that you do not want anyone else using it. You should also clearly state this in the source code and on the Web page that contains it.
If you've developed many HTML pages, you will recognize that HTML itself has this same problem. Although there's usually no harm in copying an HTML technique from a Web page-such as using <UL> for a list-some unscrupulous users have copied entire Web pages and labeled them as their own. For this reason, it's always a good idea to place a copyright notice on important Web pages-with or without JavaScript.


** If you like this post please like our page in facebook


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More