My Profile

Friday, January 21, 2011

Combining JavaScript with HTML



Java applets are compiled and stored on the server as byte codes, but JavaScript programs are simple ASCII text files. You can keep them as separate files or include the JavaScript functions within an HTML page.

The <SCRIPT> tag, an extension of HTML supported by Netscape, enables you to include one or more JavaScript functions in the page. Listing 1.1 is a very small JavaScript script embedded directly within HTML. You take a more detailed look at the syntax of these tags later in this chapter.




Listing 1.1. (SIMPLE.asp) A simple JavaScript program within an HTML document.

<HTML><HEAD> <TITLE>Simple JavaScript Example </TITLE> </HEAD> <BODY> HTML Text goes here. <SCRIPT LANGUAGE="JavaScript"> document.write("Here is my output.") </SCRIPT> </BODY></HTML>



An alternate option called an event handler enables you to specify a JavaScript action that will be performed when an event occurs. For example, a button on the page might have an action performed when it is pressed. This provides a more dynamic method of accessing JavaScript.
Instead of the <SCRIPT> tag, an event handler is added as an attribute to an HTML tag. As an example, the following HTML code defines a link with an event handler: <A HREF="www.netscape.com" onClick="alert('This will take you to Netscape's home page.');">
In this case, the name of the event is onClick. This particular event happens when the user clicks on the link. The JavaScript code to perform when the link is clicked is enclosed within double quotation marks.


Simplified Language Structure


The limitations of JavaScript also make it much easier for the programmer. The syntax of the JavaScript language is more relaxed than that of Java, and variables (names used to store values) are easier to use. Here are the specifics:
  • The JavaScript language is interpreted rather than compiled. Changing a script is as simple as changing an HTML page.
  • Rather than creating objects and classes, you can do quite a bit by simply accessing existing objects in JavaScript.
  • Variables are loosely typed: You do not need to declare variables before their use, and most conversions (such as numeric to string) are handled automatically.
  • Event handlers enable a JavaScript function to execute whenever an action is performed on part of the HTML document. For example, a form's input field can be checked for certain values whenever it is modified.


Web Browser Integration


JavaScript is an object-oriented language. This simply means that it can use objects. An object is a more complicated version of a variable. It can store multiple values and can also include actual JavaScript code. You can create your own objects to represent just about anything.
JavaScript also includes objects that enable you to access features of the browser directly. These objects represent actual elements of the browser and the Web page, such as windows, documents, frames, forms, links, and anchors.
You can access information about the links, anchors, and form elements in the current page. You can also control the browser with JavaScript. For example, it's possible to include a "back" button on your page that will send the user back to the previous page-just like the browser's back-arrow button.

Of course, when I say "browser" here, I'm talking about browsers that support JavaScript. Netscape Navigator began supporting it in version 2.0b1, and at this writing, version 3.0b6 is the latest version. It will also be supported by Microsoft Internet Explorer.

Supported on More Platforms


Because JavaScript is supported by Netscape on all the available platforms, it is supported more widely than Java. Until Java becomes more widely supported, using JavaScript for simple applications enables you to reach a wider audience on a wide variety of platforms. Currently, the only browser that fully supports JavaScript is Netscape Navigator. This functionality is provided on the following platforms:
  • Windows 3.1, using the 16-bit version of Navigator
  • Windows 95 or Windows NT, using the 32-bit version of Navigator
  • Macintosh System 7 or higher, using the Macintosh version
  • Most versions of UNIX, using the X Window version of Navigator
Note
At the time of this writing, Microsoft has released a beta version of its Web browser, Microsoft Internet Explorer version 3.0, which includes support for JavaScript. However, it is still under development and currently has trouble with some JavaScript programs.
Although this is a wide range of support and should enable about 90 percent of the Web's audience to use your script, but don't forget the other 10 percent. Whenever possible, be sure to provide an alternative for non-JavaScript browsers.
Caution
Although JavaScript is supported on these platforms, full support is provided by a beta version of Navigator at this writing. This means that it is still subject to bugs. "Debugging JavaScript Programs," for further information.


----------------------------------------------------------

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

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More