T3L10

JavaScript

Introduction

This lesson is designed for you to gain some basic information about JavaScript. When you finish this lesson, you should be able to:

  • Define JavaScript.
  • Explain the advantages and disadvantages of using JavaScript.
  • Decide when and when not to use JavaScript.
  • Utilize JavaScript in an HTML document.

Originally called LiveScript, JavaScript is a cross-platform, object-oriented scripting language created by Brendan Eich of Netscape. JavaScript consists of two components - Client-side JavaScript, and Server-side JavaScript. Core JavaScript is the combination of both client and server-side JavaScript. Core JavaScript encompasses all of the statements, operators, objects, and functions that make up the basic JavaScript language. This lesson will discuss core JavaScript, calling it simply "JavaScript."

JavaScript allows the web page developer to add some interactivity and conditional behavior to web pages. Using JavaScript you can:

  • Create mouse rollover effects on buttons, text, and images.
  • Make forms interactive.
  • Display additional information about links.
  • Create pop-up messages.
  • Change the contents of pages based on certain conditions.
  • Create and load content into new browser windows.
  • Perform calculations and manipulations on text and numbers.
  • Work with CSS and DHTML to create even more effects.

This is by no means an exhaustive list of what you can accomplish with JavaScript; it is merely a list of the most common things JavaScript is used to accomplish. Literally thousands of JavaScript examples exist.

If you decide to use JavaScript in your web pages, there are some basic issues you should know.

  • Advantages and Disadvantages of Using JavaScript
  • Example of JavaScript in Action
  • How Do I Include JavaScript in an HTML Document?
  • When Should You Use JavaScript?

Additional Resources

Webmonkey - JavaScript

[[

<devhead> - JavaScript

[[

webreview.com - JavaScript

[[

JavaScript Developer's Central

[[

]]

Microsoft Scripting Technologies

[[

JavaScripts.com

[[

The JavaScript Source - Cut & Paste JavaScripts

[[

Netscape's JavaScript Reference

[[

The JavaScript Planet

[[

Voodoo's Introduction to JavaScript

[[

JavaScript Authoring Guide

[[

Doc JavaScript

[[

Advantages and Disadvantages of Using JavaScript

Advantages

JavaScripts are scripted commands - text statements - usually embedded in the <HEAD> section of an HTML document. Thus, JavaScript loads when the page does, and loads quite quickly.

Once JavaScript loads, calls back to the server are (usually) not required. This allows the web page to dynamically respond to the user with no additional signal traffic.

Dozens of JavaScript libraries containing thousands of JavaScript examples are available on the web, making it easy to find a script either that meets your needs or can be easily adopted to meet your needs. Most of these scripts are in the public domain.

Disadvantages

Anyone can see the JavaScript source code by viewing the HTML document source. Thus you could not create a secure multiple-choice test in JavaScript; it would be very easy to see the answers!

Even though JavaScript is a standard, different browsers interpret JavaScript differently, and some commands that work fine on one browser simply do not work on another. You must carefully test your JavaScript code on all browsers to make sure it runs correctly.

If someone turns JavaScript off (via their Preferences), your script(s) will not execute. People may do this for several reasons. Have you ever closed a window in a browser, only to have another one appear? Or visited a site where windows start popping up all over the place? Usually these windows are created by using JavaScript. Turn JavaScript off and the "problem" goes away. Another reason people turn JavaScript off is to prevent client-side setting of Cookies, tiny files that store information about you.

How Do I Include JavaScript in an HTML Document?

JavaScripts reside in two places in an HTML document. The main functions and routines usually reside in the <HEAD> section, and the calling statements that activate those routines usually reside in the <BODY> section. Here is a simple HTML document example:

<html>

<head>

<script language="JavaScript"> //This tells the document that JavaScript starts here.

function displayMessage (message) //A typical function. Notice the brackets.

{

alert(message);

}

</script> //This tells the document that JavaScript stops here.

</head>

<body>

<a href="#" onClick="displayMessage('Greetings, IST fan! Now click OK to close this box.')">Click here</a> to see a message.

</body>

</html>

In most cases, you can examine an HTML document that has the JavaScript code you need, and thus determine how to place that code in your document. You will most likely have to edit raw HTML to do this; most WYSIWYG editors handle the inclusion of JavaScript poorly.

When Should You Use JavaScript?

JavaScript can add interactivity to a web page, from fairly passive mouse rollovers to aggressive form validation. If you have the need for such interactive capabilities, you should consider JavaScript in relation to competing technologies, such as CGI scripts and Java applets [[link to appropriate lessons here]]. If you are not concerned about people viewing the script's source code, and JavaScript can accomplish the task for you on the browsers and platforms you must work with, then it is the recommended choice, for it reduces traffic to and from the server. If security is an issue, then JavaScript must be discarded in favor of more secure environments, such as CGI scripts or Java applets.

JavaScript Summary

This lesson is designed for you to gain some basic information about JavaScript. When you finish this lesson, you should be able to:

  • Describe what JavaScript is.
  • Explain the advantages and disadvantages of using JavaScript.
  • Decide when and when not to use JavaScript.
  • Utilize JavaScript in an HTML document.

A short summary of these topics are listed below. If you do not understand these things, you should review the lesson at least once. If you are still having difficulty, you should consider other sources of information that compliment this lesson, such as textbooks, tutors, and instructors.

JavaScript

JavaScript allows the web page developer to add some interactivity and conditional behavior to web pages. Using JavaScript you can:

  • Create mouse rollover effects on buttons, text, and images.
  • Make forms interactive.
  • Display additional information about links.
  • Create pop-up messages.
  • Change the contents of pages based on certain conditions.
  • Create and load content into new browser windows.
  • Perform calculations and manipulations on text and numbers.
  • Work with CSS and DHTML to create even more effects.

Advantages and Disadvantages of Using JavaScript

Advantages

  • JavaScript loads when the page does, and loads quite quickly.
  • Calls back to the server are (usually) not required.
  • Thousands of free JavaScript examples are available on the web.

Disadvantages

  • Anyone can see the JavaScript source code
  • Different browsers interpret JavaScript differently.
  • If someone turns JavaScript off, your scripts will not execute.

When Should You Use JavaScript?

  • When you need to add minor interactivity.
  • When security is not an issue.

1