中国开发网: 论坛: 程序员情感CBD: 贴子 696377
张秋桥: Inside Dynamic HTML里面的一个例子。用windows来调用另一个窗口里的函数。
Creating a Window Manager
When a document in a browser creates another window, the only reference to the window is the variable returned by the open method. The object model does not expose a collection of open windows. Such a collection would be useful, for example, if you wanted to query for the existence of a particular window or to change the URL of a window.

The following code shows you how to implement your own windows collection containing references to all the windows your document has opened. This collection is analogous to the window's frames collection, which is discussed in the next section. However, due to the way variables work, the windows collection is accessible only for the lifetime of the document and is automatically cleared when the user navigates away from the page.

The code defines a method named createWindow that opens a window and adds a reference to it in the windows collection. Through this collection, you can query whether a window is open, change the contents, or close the window. When the document is unloaded, all windows created using createWindow are automatically closed.

<HTML>
<HEAD>
<TITLE>Window Manager</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// Create an array to hold references to the child windows.
/* Each member of this array will be a window object created
using the createWindow method below. */
var windows = new Array();

function newWindow(url, wname) {
// Constructor for the window
/* This function should be called only by the createWindow
function below. */
var features = "";
if (null != arguments[2])
features = arguments[2];
return window.open(url, wname, features);
}

function createWindow(url, wname) {
// Add a window to the windows collection.
var features = arguments[2] == null ? "" : arguments[2];
windows[wname] = new newWindow(url, wname, features);
}

function closeWindows() {
// Close all windows opened by addWindow.
/* To close an individual window,
its close method is called. */
/* This function should be called during the onunload
event to automatically close all open windows. */
for (w in windows)
if (!windows[w].closed)
windows[w].close();
}

/* The following two functions demonstrate using the
createWindow and closeWindows methods. */

function listWindows() {
// List the windows and their current states.
var swin = "Window List\n";
for (w in windows)
swin += w + ":" +
((windows[w].closed) ? "Closed" : "Open") + "\n";
alert(swin);
}

function openSampleWindows() {
// Open two windows.
createWindow("closeme.htm", "ChildWindow1");
createWindow("closeme.htm", "ChildWindow2");
}
</SCRIPT>
</HEAD>
<BODY ONUNLOAD="closeWindows();">
<H1>Window Manager</H1>
<FORM>
<INPUT TYPE=BUTTON ONCLICK="openSampleWindows();"
VALUE="Add Windows">
<INPUT TYPE=BUTTON ONCLICK="listWindows();"
VALUE="List Windows">
<INPUT TYPE=BUTTON ONCLICK="closeWindows();"
VALUE="Close Windows">
</FORM>
</BODY>
</HTML>

This window manager works well for named windows. If you create several windows using the createWindow method but pass empty strings for their names, the window manager will lose track of all but the most recently created window.

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录