Closing with onUnload
To close a popup when the primary window file (the opener) unloads, use a JavaScript function call in the tag of the opener. This remedies the pervasive design flaw of site builders not cleaning up after themselvesthe site's @#!*%$ popups are still lurking around when users leave. More than just a user inconvenience, many popups have relevance for a specific page within a site and should be closed when that page is exited. I wrote the code in this next example for eMirage [ designer imitations ], a retail site specializing in generic versions of name-brand products. Here, when users leave a product-specific page any open popups launched from that page automatically close.
Click example 4, then click your browser Reload button!
This code is both unique and temperamental.
Function Definition:
var faux = null;
function copyCat(url,w,h) {
faux = open(url + ".htm", "popUp", "resizable, top=40, left=40, width=" + w + ",height=" + h);
}
function flush() {
if (faux != null && faux.open) faux.close();
} Since can query the value of before it has (or no longer has) a value, ensure that you set the global variable to . Suppose a user didn't open the popup, or had opened then manually closed the popup, this code effectively queries such a status (error-free) when the user leaves the page.
Seasoned JavaScript scripters will notice a coding anomaly in an as-yet-undocumented (I'd gladly be corrected on that point) window property. Odd as it may seem, if you don't include this ghost property, you risk generating an error message when users unload the opener, especially if users have opened then manually closed the popup.
Function Invocation:
onUnload="flush()" As noted above, place the function call in the tag of the opener.
Don't confuse the code in the above example with the feature in the JavaScript 1.2 window method. This Netscape-specific feature creates a parent/child relationship between a new window and the opener window objectnot the opener file as in our onUnload example.
Adding to the litany of window features in the method lets Netscape Communicator close a child window when the parent window closes. But keep in mind that in JavaScript frames are window objects too. So depending on your site design, can create different user experiences. Suppose you launch a new window from a file in a non-frames site, the new window would remain open until the primary browser that launched it closesno matter how many other sites were subsequently visited. If on the other hand you launch a new window from a file in a frameset, the new window would remain open until the framesetnot necessarily the frame opener file since any number of files can open in a given frameunloads. It seems that to accommodate the functionality in our onUnload example a new feature for the window method should be created. "Co-dependent" has a nice ring.
Note that you can also automatically close a child window when a dependent or message closes.
Closing with conditional statements
Code conditional statements that will close the popup when certain conditions exist. Consider a transitional popup I developed for Novell's Web-compatible documentation. The popupwhich provides links to various Novell Web sitesopens a third window. Note the code in the popup .htm file that closes the Novell Web Links popup: var whoAmI = navigator.appName;
function findMe(url) {
var overHere = open(url, "w3Links", "scrollbars, resizable, top=40, left=40, height=340, width=640");
if (overHere != null) window.close();
if (whoAmI == "Netscape" && overHere != null) overHere.focus();
}
This code queries whether variable (the tertiary window) has a value (i.e., whether window is open). If so, the transitional popup closes.
Click example 5!
Function also includes code for controlling the tertiary window's focusbut it's not without its cross-browser quirks. This type of code shifts control of window functionality from the browser to the developer. With that convenient segue, our next installment will focus (no pun intended) on designing ultra-customized windowed UIs for the Web, Go to Part Two: Kiosk Popups.
Brent Lee Metcalfe is a corporate information architect for Novell Inc. Among other things, he designed and engineered the Web-based documentation user interface for several Novell® products, including Novell BorderManager, Z.E.N.works, and forthcoming NetWare® 5.0. Brent has been published in c|net's builder.com, and he runs im@go w3 design in his spare time (whatever that is!). He can be pestered here or there. Portions Copyright © 1998 Novell Inc. All rights reserved. Used with permission.