Create multiple popup windows and keep a reference

本文介绍了一种使用JavaScript创建并管理多个弹窗的方法。通过构造自定义对象存储每个弹窗的唯一名称及其对应的窗口对象,并将这些对象存入全局数组中以便随时访问和关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Transshipment: http://www.sitepoint.com/forums/showthread.php?t=45412

I am trying to write a script that will allow a window to create multiple popup windows and keep a reference to each popup window by populating an array. Then at anytime I want to access one of the child windows I can by finding it in the array and using that window object.

To make sure I can do this I made this script below which opens a page with some open window links and close window links. I should be able to open both windows and close both windows using the appropriate window objects from the array.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>


<script>
<!--

/*----------------------------------------------
In this script, I can create as many new windows as I want
by just creating a link on the page that executes the
openWindow(winURL, winName, winFeatures) function. This in turn
creates a custom object that I defined by calling the popUpWindow
constuctor.

My custom popUpWindow object has 2 properties:
1) unique name of window
2) the actual window object itself.

Then I put that new custom object into an array
object (newWindowsArray) which is Global in scope which allows
me to refer to it anywhere in the code.

Doing this let's me dynamically create new windows but still have
a handle on each window object allowing me to call any of its
methods. For example, I wouldn't be able to close the new window
from the parent window without this object handle.

These scripts are generic which allows me to put it inside a larger
script library file for including in any page that needs
this type of script.
----------------------------------------------*/

//create a global array object to hold custom poppUpWindow objects
var newWindowsArray = new Array();

function popUpWindow (winName, windowObject){
this.name = winName;
this.winObject = windowObject;
}

function openWindow(winURL, winName, winFeatures){
var newWindow = window.open(winURL, winName, winFeatures);
var win = new popUpWindow(winName, newWindow);
newWindowsArray[newWindowsArray.length] = win;
alert('newWindowsArray.length'+ newWindowsArray.length);
newWindow.focus();
}

function closeWindowByName(winName){
for(i=0; i<newWindowsArray.length; i++){
if(newWindowsArray[i].name==winName){
newWindowsArray[i].winObject.close();
break;
}
}
}

//-->
</script>
</head>

<body>

<a href="javascript:openWindow('http://www.iteye.com', 'window1', 'height=300,width=300')">open window1</a>
<a href="javascript:openWindow('http://www.iteye.com', 'window2', 'height=300,width=300')">open window2</a>
<br><br>
<a href="javascript:closeWindowByName('window1')">close window1</a>
<a href="javascript:closeWindowByName('window2')">close window2</a>


</body>
</html>


Note: Cannot Add windows object into a array directly. You will get a "Access is denied" in IE.
function openWindow(winURL, winName, winFeatures)
{
newWindow = window.open(winURL, winName, winFeatures);
newWindowsArray[newWindowsArray.length] = newWindow;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值