Today is:
    | Home | Web Technology | Language | Articles | | About-Us | Contact-Us |

JS Tutorial     

JavaScript Ch10 - JavaScript in Web Browsers


What in this chapter:

10.1 The Web Browser Environment

To understand the client-side JavaScript, you must understand the conceptual framework of of the programming environment provided by a web browser. The following sections introduce three important features of that programming environment:
  • The Window object that serves as the global object model that forms a text for client-side JavaScript code
  • The client-side object hierarchy and the document object model that forms a part of it.
  • The event-driven programming model

10.1.1 The Window as Global Execution Context

The primary task of a web browser is to display HTML documents in a window. In client-side JavaScript, the Document object represents an HTML document, and the Window object represents the window (or frame) that displays the document. While the Document and Window objects are both important to client-side JavaScript, the Window object is more important, for one substantial reason: the Window object is the global object in client-side programming.

The Window object is the global object. The Window object defines a number of properties and methods that allow us to manipulate the web browser window. It also defines properties that refer to other important objects such as the document property for the Document object. Finally, the Window has two self-referential properties, window, and self. You can use either of these global variables to refer directly to the Window object.

Since the Window object is the global object in the client-side JavaScript, all global variables are defined as properties of the window. For example, the following two lines of code perform essentially the same function:

var answer = 42; // Declare and initialize a global variable
window.answer = 42; // create a new property of the Window object
The Window object represents the web browser window or a frame within a window. To client-side JavaScript, top-level windows and frames are essentially equivalent. It is common to write JavaScript applications that use multiple frames, and it is also, if less common, possible to write JavaScript applications that use multiple window. Each window or frame involved in an application has a unique Window object and defines a unique execution context for client-side JavaScript code. In other word, a global variable declared by JavaScript code in one frame is not a global variable with a second frame. However, the second frame can access a global variable of the first frame.

10.1.2 The Client-Side Object Hierarchy and the Document Object Model

We've seen that the Window object is the key object in client-side JavaScript. All other client-side objects are connected to this object. For example, every Window object contains a document property that refers to the Document object associated with the window and a location property that refers to the Location object associated with the window. A Window object also contains frames[] array that refers to the Window objects that represent the frames of the original window. Thus, document represents the Document object of the current window, and frames[1].document refers to the Document object of the second child frame of the current window.

An object referenced through the current window or through some other Window object may itself refer to other objects. For example, every Document object has forms[] array containing Form objects that represent any HTML forms appearing in the document. To refer to one of these forms, you might write:

 
window.document.forms[0];
To continue with the same example, each Form object has an elements[] array containing objects that represent the various HTML form elements (input fields, buttons, etc.) that appear within the form. In extreme cases, you can write code that refers to an object at the end of a whole chain of objects, ending up with expressions as complex as this one:
parent.frames[0].document.forms[0].elements[3].options[2].text;
We've seen that the Window object is the global object at the head of the scope chain and that all client-side objects in JavaScript are accessible as properties of other objects. This means that there is a hierarchy of JavaScript objects, with the Window object at its root. Figure 10–1 shows this hierarchy.

The Event-Driven Programming Model

Programs are generally event driven; they response asynchronous user input in the form of mouse-clicks and keystrokes in a way that depends on the position of the mouse pointer. A web browser is just such a graphical user interface (GUI), so client-side JavaScript uses the event-driven programming model.

It is perfectly possible to write a static JavaScript program that does not accept user input and does exactly the same thing every time. Sometime this sort of programming is useful. More often, however, we want to write dynamic programs that interact with the user. To do this, we must be able to respond to user input.

In client-side JavaScript, the web browser notifies programs of user input by generating events. There are various types of events, such as keystroke events, mouse motion events, and so on. When an event occurs, the web browser attempts to invoke an appropriate event handler function to respond to the event. Thus, to write dynamic, interactive client-side JavaScript programs, we must define appropriate event handlers and register them with the system, so that the browser can invoke them at appropriate times.

If you are not already accustomed to the event-driven programming model, it can take a little getting used to. In the old model, you wrote a single, monolithic block of code that followed some well-defined flow of control and ran to completion from beginning to end. Event-driven programming stands with this model on its head. In event-driven programming, you write a number of independent (but mutually interacting) event handlers. You do not invoke these handlers directly, but allows the system invoke them at the appropriate times. Since they are triggered by user's inputs, handlers will be invoked at unpredictable, asynchronous times. Must of the time, your program is not running at all, but merely sitting waiting for the system to invoke one of its event handlers.


    References

    (1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.

    (2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code

    (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software.

    Advertisement

puthik.com ©2008