Create multiple popup windows and keep a reference

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章