11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong>re are two primary advantages that attached Behaviors have over traditional DHTML. <strong>The</strong><br />

first is that attached Behaviors are easier to add to your pages and are easier to maintain once<br />

they have been added. <strong>The</strong> second is that Behaviors are more encapsulated and reusable than<br />

most traditional DHTML applications. While traditional DHTML can certainly be written in a very<br />

modular fashion, it is generally easier to create reusable components using attached Behaviors.<br />

In addition, Behaviors are easily used in combination on the same element. This is a feature<br />

even well-written traditional code often lacks.<br />

Element Behaviors permit functionality that would otherwise be impossible (or very hard to<br />

obtain). <strong>The</strong> ability to extend your documents with your own custom elements gives you<br />

freedom in design and implementation.<br />

<strong>The</strong> downside of Behaviors has already been mentioned, namely, that they are a proprietary<br />

technology not yet a part of any standard. But, then again, a large number of DHTML<br />

applications are written to use proprietary Document Object Models, so Behaviors do not mark<br />

all that significant of a departure from traditional trends.<br />

Pop-up Windows<br />

Internet Explorer provides the ability to create pop-up windows using the createPopup()<br />

method of the Window object. This capability was touched upon in Chapter 12, but is included<br />

here for completeness.<br />

<strong>The</strong> behavior of pop-up windows is different than that of windows created with window.open().<br />

<strong>The</strong> createPopup() method accepts no arguments and returns a reference to a window that<br />

was created. <strong>The</strong> newly created pop-up window is initially empty and hidden and is not<br />

immediately given focus. <strong>The</strong> programmer is responsible for populating the window with content<br />

and then displaying it to the user with its show() method. <strong>The</strong> pop-up menu is then<br />

automatically hidden once the user activates another part of the page, for example, by rightclicking<br />

outside of the pop-up menu.<br />

<strong>The</strong> syntax of the show() method is<br />

popupWindow.show(x, y, width, height [, relativeTo])<br />

where popupWindow is a reference to a window created with window.createPopup() and x, y,<br />

width, and height specify the horizontal location, vertical location, width, and height of the popup<br />

window in pixels. <strong>The</strong> optional relativeTo parameter is a reference to the object to which the<br />

x and y coordinates are relative. If relativeTo is omitted then the x and y coordinates are treated<br />

as relative to the upper-left corner of the main window.<br />

Creating an example pop-up window gives us a good excuse to exercise the conditional<br />

compilation features mentioned earlier in the chapter. Because pop-up windows are available<br />

only in Internet Explorer 5.5+, one would only be used if supported by the browser. Otherwise,<br />

an alert() box would be used, although it would probably be better to use a relatively positioned<br />

DHTML layer in a ―real‖ application. <strong>The</strong> pop-up menu displayed in IE5.5 is shown in Figure 21-<br />

6.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!