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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

popup window close itself once parent window is closed

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
CA
Hi,

I have a popup winow that reloads itself every 3 secs, is it possibe to have code in that popup to see if parents window is stil there and if parent is close , it close itself ..

thanks
 
If the reload is via some js handler, then, in the handler, add a little block of script to check for opener closed property.
[tt]
if (!window.opener || window.opener.closed) {
//popup window close-out functionality here
}
[/tt]
 
thanks a lot

I also have a popup window that reloads itself every 3 secs, is it possibe to have code in that popup it close itself after reloading 30 times, does javascript have global variable which can be used for counting.

thanks
 
>... does javascript have global variable which can be used for counting.
It has global variable all right, but, the variable won't persist page after page in the reloading process. In order to make client-side aware of such data, you can add an alien query variable to the location url. The variable name should be alien semantically to the server-side logic. Here, I mean if the query variables server-side interpreting are named (x,y,z,...), you choose a specific variable outside of that set, say a. And then you append to the location url with a=1 as query in the client-side script for the first reload, a=2 for the second reload. That data will be client-side-ready. Once it reaches 30 (as spec), you stop reload routine and that's done.
 
i understand whay you said

suppose i first time load with URL

1st

2nd

3rd

then how in my javascvript how I will retrive the value of
mycounter

do I need to do something like the following?
Code:
var url_string = String(document.location);
var url_variable = url_string.split('?');

or is there a better way?
 
[0] There are some built-in functions to handle url parsing, but, for some specific scanning of it, it is usually not a click of figures, you still need to do some work on it. I prefer using regexp my way - not for anybody else. In this particular circumstance, you can proceed as outline below.

[1] Make some global variables fixing the parameters, somewhat self-explanatory.
[tt]
var qvar="mycounter";
var ntimes=0;
var nmax=30;
var tlag=3000; //3 sec
[/tt]
[2] In the handler, parsing the url and reload with script like this or any variation of it.
[tt]
var url_string=window.location.toString();
var rx=new RegExp("^(.*)(\\?|&)("+qvar+"=)(\d+)(&.*|$)","i");
if (rx.test(url_string)) {
ntimes=parseInt(RegExp.$4,10)+1;
url_string=url_string.replace(rx,"$1$2$3" + ntimes + "$5");
} else {
ntimes=1;
url_string=url_string+((/\?/.test(surl))?"&":"?")+qvar+"=1";
}
if (ntimes<nmax) {
setTimeout("window.location='"+url_string+"';",tlag);
}
[/tt]
 
Amendment
I've typed one backslash short. The corresponding line should read like this.
[tt] var rx=new RegExp("^(.*)(\\?|&)("+qvar+"=)([red]\[/red]\d+)(&.*|$)","i");
[/tt]
 
Amendment-2
I have another typo (translating to your convention) in this corresponding line which should be read as below.
[tt] url_string=url_string+((/\?/.test([red]url_string[/red]))?"&":"?")+qvar+"=1";[/tt]
 
Risk to get real confusing for two amendments, here is the complete relisting.

[2-rev] In the handler, parsing the url and reload with script like this or any variation of it.
[tt]
var url_string=window.location.toString();
var rx=new RegExp("^(.*)(\\?|&)("+qvar+"=)(\\d+)(&.*|$)","i");
if (rx.test(url_string)) {
ntimes=parseInt(RegExp.$4,10)+1;
url_string=url_string.replace(rx,"$1$2$3" + ntimes + "$5");
} else {
ntimes=1;
url_string=url_string+((/\?/.test(url_string))?"&":"?")+qvar+"=1";
}
if (ntimes<nmax) {
setTimeout("window.location='"+url_string+"';",tlag);
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top