Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for an open window

Status
Not open for further replies.

Sarky78

Programmer
Joined
Oct 19, 2000
Messages
878
Location
GB
Hi

I have got an advertisment page that will either open a new window if the advert is external to the website or change the current website page if the link is internal. What i want to be able to do is to check if the main website window is still open before executing the link to the internal web page to stop a javascript error that appears.

What i have at the moment is this:

function openAdvert(URL)
{
window.opener.location=URL;
window.opener.focus();
}

if the opener window has already been closed this is where i get the javascript error. Can i check to see if this window is still open ?

Thanks in advance
 
hi
we have closed property:
if (!opener.closed){
...
} Victor
 
hi

tried that and it doesn't work, I used the following code and got an exception error on the !.opener.closed line

if (!opener.closed)
{
window.opener.location=URL;
closeup()
window.opener.focus();
}
else {
alert("Need to open a new window");
}

 
Apparently you can also use an empty open call to get a reference to an already open window.


var openWindow = open();
if(openWindow) // window open already.

See if it works.
 
Yep, that'll let you know that the window got opened, but if you want to check to see if it still exists try this one:

var openWindow = window.open(URL,"openWindow","");
openWindow.focus();

if (openWindow && openWindow.open) ...

You'll get an error if you check openWindow.open if openWindow doesn't exist, so you need to check that first.
 
aprerry, what error exactly have you got?

this stuff worked for me allways, & it works now (ie6, nc4.61, nn6 win98) it failed in opera 5.12, but it wasn't surprising for me..

what browser did you used?

(i've checked with this function: )

function chfth(){
if (!opener.closed){
alert(opener+" "+opener.closed)
}
else alert("closed")
} Victor
 
Victor

regardless of if the opener window is still open or not i get an alert saying [object] false indicating that the window is closed even when it is open.

I am using ie 5 on win NT 4

Cheers

Tony
 
no, wait, if i got it right, closed=false means that window is NOT closed (it is NOT true, it is FALSE)

so, alert(opener+" "+opener.closed)
would give you [object] false if opener is not closed, & otherwise (if the opener was closed, according to my test function) you'll get closed alert box Victor
 
what you say is correct, if the window is still open then i will get [object] false, but i am also getting this when the window is closed!!!

can you tell me a website where yo have used this script, unfortunatly i can't provide a webaddress for the page this is failing on as it is behind a load of security at the moment
 
well, actually, i didn't used it.. as i told, it was for some guy (Oleg, as i remember)

but i can upload it to my server, & you'll checkif it would work for you...

i'll post here the url in a few minutes.. Victor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top