close child page

onepieceking

Well-known member
Joined
Sep 20, 2006
Messages
64
Programming Experience
1-3
Hi,

I am facing a problem of not being able to close a browser window.

1) I have a web page (test1.aspx) that has 2 asp.net buttons (open and close). This web page also have a meta tag that refresh the page every 2 sec.
2) When I click on the "open" button, I will use a RegisterStartupScript to use javascript to open a child window (test2.aspx). "test2.aspx" also has a meta tag that refreshes the page every 2 sec.
3) When I clicked on the "close" button in "test1.aspx", it is supposed to close the child window.

Because of the refreshing, I think the referencing between the 2 pages is gone, therefore using javascript to close is impossible.

I tried to use GetProcessByName("iexplore") to find all the browsers. However, it seems that the child window is not found. Why is that so?

Is there a way for me to close the child window?
 
I'm not sure if you can easily control the child window from the main window but you might consider just putting the close button on the child window. If you want more control you may have to use Java or ActiveX as opposed to Scripting. But I am pretty new to this all myself so we'll see.
 
Hi,

I have thought of a way to close the child from the main. Since the child and main both share the same session, i will check the session each time they refresh.

When the main edit the session, then the child should be able to see it at the next refresh. I will then insert a response.write which will call a javascript of "window.close". In this way, the main can close the child.
 
Here is the correct answer....

When you open a new window via JavaScript your should name the new window like so...

VB.NET:
//This is the code open the window notice in the parameters we name the new window NewWin
<input TYPE="button" VALUE="Open New Window" onClick="NewWin=window.open(' ', '[COLOR=red]NewWin[/COLOR]','toolbar=no,status=no,width=200,height=100'); ">
 
//Now the code that calls that window by name and closes it
<p><input TYPE="button" VALUE="Close Window" onClick="[COLOR=red]NewWin[/COLOR].close(); " ></P>

This was copied pretty much word for word out of a book I am reading titled "JavaScript in 24 Hours" written by "Michael Moncur". It was only $25 at Barnes and Nobles in the US and I really suggest it. However; I am surfing through this book b/c I have very good programming knowledge and experience. If you are newer to programming entirely a more in depth book will be a better choice. Anyways, that code will open a new window named NewWin and close that same window via two seperate buttons.
 
In all those examples it is not the window title that is the key (as marked) but the Javascript variable that have to be declared global (var NewWin;) This is the window handle used when calling NewWin.close(). This operates client-side, so once a postback happens (with refresh etc) the client code breaks. I haven't found a way to persist this window handle after postback, but calling the same window.open will reuse existing window and new handle is returned, so that handle can be used to close existing window after postback.

Also the window title can be used for close by calling window.open with javascript code to close it by same command, the Javascript code is very simple, just one line here:
HTML:
window.open("javascript:window.close()", "newWin", "");
 
Hi,

I think that will work just fine. Thanks man. I din thot of that one. Using the session may do the same thing, but it will be slower since it is checking on the value of the session and will only execute upon refresh.
 
Ahh, my bad..... I just re-read the first post and realized the problem was during a refresh. My bad but good to know the real solution anyways, I wouldn't have had that answer.
 
Using the session may do the same thing, but it will be slower since it is checking on the value of the session and will only execute upon refresh.
No, Session is server-side only so it can't be used client-side.
 
Oops.. I din explain myself.

What I did was that I will change the value of a session upon clicking the close button in the main window. The child window will check the session and if the session value is "closed", it will call a javascript to close window. It does help me to close the window, but because of the checking and refresh, it is slower.
 
I see, but that may never happen because the child may never postback. I re-read your first post now and also see that you set the child page to postback every 2 seconds, so in this case you are right it should work but a little slow.
 
Back
Top