Introduction
Recently, I have been doing a great deal of web development. Because of which, I decided to create an API set for the more commonly used functions I routinely needed to implement in JavaScript. This article contains two functions from my API set that may be of interest to web developers at large.
These functions wrap the process of creating and destroying popup windows. The benefit from using this is that it automatically centers your popup gracefully. By gracefully, I mean that it will center when possible and leave the window be if not possible without producing an error message to the user. Typically, most have their popups only centered on the 4.0+ (JavaScript 1.2+) browsers, but this goes one step further and centers the popup on Netscape 3.0 as well by accessing the AWT from JavaScript.
The two functions involved here are CreateWnd() and DestroyWnd(). As you might imagine, CreateWnd() will either create a popup window or replace an existing one you might have created already and DestroyWnd() will close the currently open popup window.
CreateWnd() takes four parameters:
file = URI of the file to open
width = width of the created popup (in pixels)
height = height of the created popup (in pixels)
resize = boolean to specify if the popup is to be resizable
DestroyWnd() takes no parameters.
NOTE: If you repeatedly call CreateWnd() without destroying the current popup prior to each call it will replace the current popup. This is by design because I dislike (as many do) having more than one popup at a time from a webpage.
Example
<script language="JavaScript" src="popup.js"></script>
<body>
<a href="javascript:CreateWnd('../somefile.html', 250, 200, true);"><BR> Open Window</a>
</body>