avaScript is a powerful yet often underused feature of most browser-based applications. Commonly, JavaScript is relegated to simple form validations, but this is not the only capability of JavaScript. In this article I’ll cover a powerful set of features you can incorporate in your applications today.
This article explores the following examples:
- A simple menu system.
- A scrollable data grid complete with column headers and row selection.
- A whiteboard using VML that shows how to create simple classes in JavaScript.
The examples (created in Internet Explorer) assume a basic familiarity with HTML. Please note that this article is an overview of JavaScript and DHTML, and except for a brief discussion of .NET, does not really address JavaScript’s place in the creation of .NET User Controls.
What Are JavaScript and DHTML and What Can They Do?
Put simply, JavaScript is a no frills and yet flexible programming language supported by all major Web browsers. Although it is supported on most Web servers, and performs server-side processing that creates a Web page you can send to a browser, its real power comes from using it to manipulate the HTML of a Web page in a client browser environment.
The browser exposes the structure of a Web page to JavaScript through a document object model or DOM. You typically refer to programming the DOM using JavaScript as using Dynamic Hyper Text Markup Language (DHTML).
Since the DOM exposes the current Web page as an object hierarchy of the pieces in the document, you can use JavaScript to manipulate its parts. If you want a section of the page to disappear for a while, find its representative object in the hierarchy and set its style visibility property to hidden. Later on you can set it back to visible. If you want to have a bouncing ball on the screen, add a new HTML IMG element, showing the ball, to the object hierarchy and reposition it every second using whatever function you want to specify for the bouncing behavior. If you want to handle a collection of items without worrying about binding events to each one, wait for events to bubble up from child to parent in the DOM hierarchy and handle them at the container level.
You can easily handle all these tasks more by using JavaScript and DHTML. In fact, JavaScript also allows you to create simple class objects, and you can reuse your favorite pieces of client-side code.
Quick Recap of the Web Server/Web Page Cycle.
JavaScript is very good at manipulating Web pages, however, Macromedia Flash or server-side code may be a better choice if you have a lot to manipulate. Since every application is different, there are no global rules, but a quick discussion of the structure of a Web page may help you understand where the blocks and difficulties will appear in your own applications.
Figure 1 shows the simplified logical architecture of the cycle for a Web browser to request and send information .to a Web Server.
![]() |
Introduction to JavaScript and Style Sheets As a first example using JavaScript I’ve created a simple menu that binds behavior to the mouse using events from the DOM. This example also introduces the topic of style sheets. HTML supports various properties such as height, width, font color, and often borders. You can set properties directly in HTML or you can use style definitions, which you’ll find are similar to styles in Microsoft Word and they’re easy to reuse. Using styles has become so prevalent in browsers that they have become the desirable way to set the look and feel. Styles have even been expanded so that they offer more options than are available in HTML itself. You can specify a style in many ways the following methods are all equivalent ways to set the font and color of a tag: (a) Attributes of a tag
(b) Inline style properties
(c) Referenced style tag properties
Style sheets provide a way to define groups of styles for your Web page. You can make them specific to certain tag names and you can apply them automatically or you can make them generic and apply them to any tag explicitly. You can include these definitions in a Web page itself using the style tag or in a separate .css file. In the latter case you can link them to the primary Web page by including a LINK tag as follows:
For example, to automatically set all SPAN tags in the page above to have a black border around them you could add the following to your style sheet:
To be specific, a tag has to declare itself with a particular class. For example, the Home button uses the Style class named CurrentOption as follows:
You can make very robust style sheets. They can include multiple levels of hierarchy and filtering. A wide variety of properties are available for you to set by using styles, including positioning, borders, fills, fonts, colors, and more. JavaScript as a Programming Language. Variables Variables in JavaScript are actually pointers to objects. You do not need to create common variable type objects with the NEW keyword because you can assign values with no additional syntax. Think of their value as their default property. Notice that the var bFound = false; declaration line in the program below creates and initializes the bFound object and sets its value to false, whereas the var i; line merely has a value of undefined. All of these built in types have type-specific methods that you can access as in the case of the bFound.toString() that does a type conversion and returns a string object. Objects Some special JavaScript core objects are static and always available. Think of these objects as the function libraries of JavaScript as they group many useful functions for easy use. For example, you can use the Math.random() method (used later in the whiteboard example) to generate a random number. You can build your own objects using containment. These objects require creation using the NEW keyword and I’ll cover them later in the whiteboard example. Functions Another key concept in JavaScript is the function. If you don’t specify a return value, JavaScript treats the function as a void function. In the following example the function receives two parameters, a string and a comma-delimited list. Local variables get defined and will have the value null until an assignment happens. The Split function is a very useful function since JavaScript does not allow you to pass an indeterminate number of parameters. Often a delimited list can replace the need to pass and manage arrays. Consider the following short program that contains the basic concepts comprising JavaScript:
You can insert comments into your code by using the double back slash '//' syntax, which tells JavaScript to ignore the rest of the line. Curly braces '{}' indicate a statement that contains code blocks. Functions and declarations use the parentheses '()'. All actual statements require an ending semi colon ';'. Now that we've explored a basic JavaScript function, I'll show you how to use functions and bind them to events in a Web page. You can use JavaScript in many places in your pages, and when Web pages run across script they may assume that your code block is JavaScript. However, it is a far better practice to be explicit. Consider the following examples that show how to bind a simple alert MessageBox to an event. (a) Overloading an anchor tag to do something other than linking. (The 'void' in the line ensures that the page does not refresh or attempt to navigate):
(b) Inline directly on the tag:
(c) Script bound to an event through the script tag attributes:
|