Window.name and "Access is Denied" error in IE


#1

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.

I have two problems with this script:

1) IE5.x gives me an "Access is denied" error on any line that tries to access the name property of a window. Why? I should be able to access this right?

2) Netscape will process the openWindow function just fine but bombs using the closeWindowByName() function. It seems to bomb on "if(newWindowsArray[i].name==winName)." I think it has to do with the name property of the window object again.

Any Help or ideas? The HTML for the document including the script is listed below

BEGIN HTML

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

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

<script>
<!--

//the new window object handle

var newWindowsArray = new Array();

function openWindow(winURL, winName, winFeatures)
{
newWindow = window.open(winURL, winName, winFeatures);
newWindowsArray[newWindowsArray.length] = newWindow;
}

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

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

<body>

<a href="javascript:openWindow('http://www.sap.com', 'window1', 'height=300,width=300')">open window1</a>
<a href="javascript:openWindow('http://www.sap.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>


END HTML


#2

Can no one here help me?

Is the answer just so simple that you don't want to tell me or is my question worded so that it is hard to understand?


#3

Hey, I came up with a solution. I don't know why I had the error before but I am now doing it a totally different way.

OK, below is the entire HTML document that contains a script you can use to dynamically create window objects and still keep a refernce to each child window in the parent window.

It is cool because it is generic and can be inserted into any document or even a larger javascript library file. Please feel free to use it if you want it.

BEGIN HTML DOCUMENT


<!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();

//constructor for the custom popUpWindow object
function popUpWindow (winName, windowObject)
{
this.name = winName;
this.winObject = windowObject;
}

// function to call in parent window when opening a window
function openWindow(winURL, winName, winFeatures)
{
// create a new window object using the input parameters from user
var newWindow = window.open(winURL, winName, winFeatures);
// create new popUpWindow object passing in the newly created
// window object's name as the first input parameter and the object
// itself as the second parameter
var win = new popUpWindow(winName, newWindow);
//populate the next index of the window array
newWindowsArray[newWindowsArray.length] = win;
// call focus() for the window object incase the window
// has already been created (you cannot create a window with
// the same name twice)
newWindow.focus();
}

// function to call in parent document when closing a window
// Pass in the name of the window you want to close as the
// input parameter.
function closeWindowByName(winName)
{
// Loop through the array and try to identify the window in question
for(i=0; i<newWindowsArray.length; i++)
{
// check each custom popUpWindow object to see if it has
// a matching name as the window the user wants to close
if(newWindowsArray[i].name==winName)
{
// use object syntax to call the close method of the
// window object. Note, newWindowsArray[i] is the same
// represents the popUpWindow custom object
newWindowsArray[i].winObject.close();
break; // end loop since you found the window
}
}
}

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

<body>

<a href="javascript:openWindow('http://www.sap.com', 'window1', 'height=300,width=300')">open window1</a>
<a href="javascript:openWindow('http://www.sap.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>


END DOCUMENT


#4

Well here's a little Info about Cross-Frame Scripting and Security - the same rules apply to popups.
It prevents you from accessing document properties(Objects), on pages that aren't on your server, and returns the error "Access denied".

I found this out after creating a generic DOM web spider that accessed meta tags cross frame, then could submit the info into a DB, would have been nice, but you can only access pages on your own sever, so that ended that frowning

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/om/xframe_scripting_security.asp


#5