JAVASCRIPT UTOPIA |
LAUNCH POP-UP WINDOWS ELEGANTLY (VERSION 1.0) |
function launchwin(winurl,winname,winfeatures) { if (newwin == null || newwin.closed) { newwin = window.open(winurl,winname,winfeatures); } else { newwin.focus(); } }Now *that* would be elegant. And theoretically, the above function is all we should need to launch or focus a window successfully.... Theoretically.
<HTML> <HEAD> <TITLE>Your Page Title Goes Here</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- //Popup Window //Version 1.0 //Last Updated: November 27, 1998 //Code maintained at: http://www.moock.org/webdesign/javascript/ //Copy permission granted for non-commercial uses. Written by Colin Moock. var newwin; function launchwin(winurl,winname,winfeatures,parentname) { //This launches a new window if one isn't already launched. If one is launched, it focuses it. //It works in Nav3+ and MSIE4+, and does not cause errors in IE3. This function requires that the following //code be called by the ONUNLOAD handler in all pages that reside in the new window: // if(!document.images){opener.newwin = null} // Use this when not specifying params from the event handler: // var parentname = "mainwindow"; // var winurl = "content.html" // var winname = "newwindow" // var winfeatures = "height=150," // + "width=300," // + "channelmode=0," // + "dependent=1," // + "directories=0," // + "fullscreen=0," // + "location=0," // + "menubar=0," // + "resizable=0," // + "scrollbars=0," // + "status=0," // + "toolbar=0," // + "screenX=0," // + "screeny=0," // + "left=0," // + "top=0"; //Only use the following attributes with a signed script: //------------------------------- // + "hotkeys=1, " // + "alwaysLowered=0, " // + "alwaysRaised=0, " // + "titlebar=0, " // + "z-lock=0, " //------------------------------- if (!document.images) //Quick sniff for IE3. If the browser is IE3, don't check to see if the //window is already open. Just pretend it's always closed. { newwin = null; } if (newwin == null || newwin.closed) //-check for the existence of the window. //-we check in two ways: 1) is the newwin object null? It should be if we've closed the window // because we set newwin to null in a function called by the onunload handler. // And 2) is the newwin.closed property true? We check this second state because Netscape 4.5 // doesn't call the onunload handler when you use the exit button on the window itself. So we // check the closed property for the benefit of Nav 4.5. Why not just check that? Because IE4 // chokes on it if you do. It's probably a good idea to check both anyway because "closed" // wouldn't be set before the first time the user launches the window. IE4 doesn't choke if we // check the status of newwin first. { //we've determined that no window is open, so launch one now newwin = window.open(winurl,winname,winfeatures); if (newwin.opener == null) // set the opener property manually for Nav 2.0. { newwin.opener = window; } newwin.opener.name = parentname; // give our parent window a name so we can target it from newwin } else { //if a window already exists, bring it to the front using windowname.focus() //(for Nav3+ and IE4+). IE3 will never get here because we force it to think the //window is always closed. IE3 always launches a new window when launchwin() is called. //Interestingly, IE3 will focus the newwin the first time we try to recreate it, but not //any subsequent times. alert("This link is already open in another window. Click OK to continue."); newwin.focus(); } } //--> </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
<HTML> <HEAD> <TITLE>Your Page Title Goes Here</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function clearvar() { if(document.images) //quickie IE3 sniffer... //IE3 doesn't know what "self.opener" is, //and gives errors when you try to assign "self" to a variable to simulate //the opener property (as in "newwin.opener = self"). So we simply don't set the //window status to closed in IE3 when this window is closed. We'll force the status to be //closed every time we launch the window from the parent's script. { //In Nav3 and IE4+, we clear the existence of newwin when newwin is closed. We do this //manually because the newwin object will continue to exist even after the physical window //is gone. In theory we should check the .closed property of newwin to determine //whether or not it's still open (eg. "if(newwin.closed)"), but IE4 returns a strange error //when that property is referenced: "The RPC server is unavailable.". So, for IE4's benefit, //we clear the newwin variable completely when the window is closed by the user. //One problem with setting newwin to null manually is that Nav4 doesn't call onUnload when the //user uses the window's close button (X) to close the window. So to accomodate Nav4 *and* IE4, //we actually end up having to check both the existence of newwin and the status of newwin.closed. //A general warning against using the onUnload handler on pre MacOS 8 Macs: If you use that //handler in conjunction with closing the browser window in Netscape 3.x, you are likely to //crash or kill the browser, and possibly even the Finder. For details of the bug, see: //http://www.strangemedia.com/dmt/onUnload/ opener.newwin = null; } } //--> </SCRIPT> </HEAD> <BODY onUnload="clearvar()"> </BODY> </HTML>