There are two aspx pages, page1.aspx and page2.aspx. There is an 'Open' button on page1, click this button, I open an separate browser window from which I open an separate browser window with url being page2.aspx. Both pages are in same domain.
On page2.aspx, every 5 seconds, it calls a javascript function that will check the "Closed" property of opener window. If the opener window is not closed, it return me false, which is correct. BUT if the opener window is closed, I am getting a permission denied Javascript runtime error.
I am running the application on IE6.
Here is the code:
For page1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<script language="javascript">
function func1()
{
window.open('Page2.aspx', 'name');
}
</script>
<body>
<form id="form1" runat="server">
<asp:Button ID="OpenBtn" runat="server" OnClientClick="func1()" Text="Open" /><br />
<br />
</form>
</body>
</html>
//For Page2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="PAGE2"></asp:Label></div>
</form>
</body>
</html>
<script language="javascript">
window.setTimeout(abc, 5000)
function abc()
{
debugger
if (window.opener.closed)
{
//do something
}
}
</script>
![]() |
0 |
![]() |
I ran into the same problem but got around it by putting the check for window.opener.closed in a try - catch block so when it errors it's most likely because it's been closed.
try { if(window.opener.closed) { //do something } } catch(err) { //something else }
![]() |
0 |
![]() |
I've tested on IE6 and FX1.0.7.
On IE, it just seems to be ok, even if the opener has been closed...
In FX instead the opener reference is null when the opener has closed.
In any case, the code might just look like:
if(window.opener == null || window.opener.closed) ...
That is, check for the opener reference not to be null. Handling exceptions in javascript is not yet widly supported i'm afraid.
-LV
Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name
(Peace X Love] = [++1)
![]() |
0 |
![]() |
I'm working in IE6 as well but had the same results as the original post.
When the opener is closed, I get the same Permission Denied error. I also tried the check for
if(window.opener == null) ... but it never seems to pass this condition, even if the opener window is closed.
That's why the only way I was able to get around it was to catch the permission denied error....tho i'm open to another way as well. In my case it's an internal web app which will run only on IE so I'm allrite, but it's good to be aware of the differences with other browsers especially with exception handling so I can prepare if we ever allow external access.
![]() |
0 |
![]() |
Ok, i don't question your results, as there might be differences with operating system setup in general.
Anyway, would you mind trying the following on your system? This might give a hint:
alert(window.opener);Let me know, this is interesting...
-LV
Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name
(Peace X Love] = [++1)
![]() |
0 |
![]() |
Ok, I tried the alert(window.opener) when the parent window was still open and when it was closed and I recevied the following text in the alert window both times: "[object]"
I also tried alert(window.opener.document.title) with the parent window open (no problems) and again with it closed (again got the "permission denied error")
This is interesting.. the window.opener always has a reference to an object, even when closed; however, I can't access the object at all.
![]() |
0 |
![]() |
It is so interesting that it behaves differently even all in IE6. I am thinking if it has to do with the windows update. I have all the latest updates installed and my operation system is XP+sp2. I am using "alcsharp"s work around solution temporarily. But unfortunately, for me, I am developing a real application. So, if anyone know other solution, let me know.
![]() |
0 |
![]() |