I have an C# aspx web application that opens a popup window with JavaScript. I capture the window handle and put that value into an array. When the web application closes, I want to close the popup window. When I interrogate the array, the window handle is no longer in the array, so I can’t find the window to close it. This behavior is strange to me, since other popup windows (that don’t contain silverlight) will remain in the array, and are closed when the application ends.
At first, I thought it was something that could be solved with a frame, like popup windows containing PDFs not being able to be closed, but the solution for that didn’t work here for me. This was something I had to use with PDFs
Question: How do I close the popup window containing Silverlight when the aspx main window closes?
Some JavaScript code:
var openedWindows = new Array();
function OpenNamedWindow(url, name, features, replace)
{
var oWin = open(url, name, features, replace);
// The Silverlight window object is within this array afterwards, and in subsequent calls
// to this method
openedWindows.push(oWin);
}
function CloseOpenedWindows()
{
while (openedWindows.length > 0)
{
var window = openedWindows.shift();
if(!window.closed)
window.close();
}
}
The Main aspx form (abbreviated)
<html>
<body onunload="CloseOpenedWindows();"> ... <body/>
</html>
Turns out that the javascript file that contained the
was being loaded multiple times, so javascript got confused as to which array it needed to iterate through. Ensuring that the js file was only loaded once solved the problem.