PDA

View Full Version : Resolved Close all pages of a website if the main page is closed



KennyP
02-24-2016, 10:25 AM
Hi:

On this website test page (http://www.reneesrosegarden.com/), if the "Contact Renee" button is clicked the contact page opens. In the event that page is not used (a message not sent),
when the visitor brows to a different website or closes the browser, the contact page stays open. Is there javascript that can close all pages of a
website if the visitor brows away from that website? That is, I'd like the "Contact Renee" page to close if the visitor leaves the website and doesn't
send a message.

I thought this might work, but it doesn't:

<script type="text/javascript">
// open new window:
var windows = [];
windows.push(window.open('http://www.reneesrosegarden.com', '_blank'));

// close them all:
for(var i = 0; i < windows.length; i++){
windows[i].close()
}
</script>

Thanks

jscheuer1
02-24-2016, 01:37 PM
Well, hmm. It might if that was how the window was opened. I don't think there's a way to guarantee what I think you're talking about. However, there is a way, in most cases, to close the contact window via the reference to it that was created when it was opened if the window that opened it is closed. So, for example on http://www.reneesrosegarden.com/ where it has:


<script language="javascript" type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,re sizable=no';
win=window.open(mypage,myname,settings);}
// -->
</script>

That's the actual code currently used to open the contact window and win is the reference it creates. So one can use win as a reference to close it if that page closes. And since jQuery is already in use, we can do that fairly unobtrusively (addition highlighted):


<script language="javascript" type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,re sizable=no';
win=window.open(mypage,myname,settings);}
$(window).unload(function(){if(win){win.close();}});
// -->
</script>

KennyP
02-24-2016, 10:21 PM
Thank you for your reply John. I tried your posted code-addition but unfortunately the Contact page doesn't close even if the website page that opened it is closed - but it should.
You can see the effect of the additional-code on the Contact page that no longer resizes nor closes here on this test page (http://www.reneesrosegarden.com/index2.php).

Also, it makes the browser re-open the initial home page as a pop-up.

Deadweight
02-24-2016, 10:34 PM
Couldn't you make a cookie so see if the main page is still up. Once you close the main page use the unload function to remove the cookie. Once they try to move the mouse it checks the cookie if it is null then close or redirect the page?

KennyP
02-25-2016, 02:59 AM
Thanks for your reply Deadweight. How would you write that out?

jscheuer1
02-25-2016, 03:09 AM
You have not made the suggested change I proposed.

KennyP
02-25-2016, 07:08 AM
<script language="javascript" type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,re sizable=no';
win=window.open(mypage,myname,settings);}
$(window).unload(function(){if(win){win.close();}});
// -->
</script>

I inserted the above code above into the main test page (http://www.reneesrosegarden.com/index2.php), but that is all I understood you suggest I do. Did you propose I do something else I didn't understand?

jscheuer1
02-25-2016, 01:35 PM
First I've seen that page. Unlike the page where I suggested you place that, this test page has invalid coding which renders that entire block of code inoperable:


<script language="javascript" type="text/javascript">
<script language="javascript" type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,re sizable=no';
win=window.open(mypage,myname,settings);}
$(window).unload(function(){if(win){win.close();}});
// -->
</script>

There could also be other problems, but that extra opening script tag must be removed before any of the code there will be used.

KennyP
02-25-2016, 10:20 PM
Thank you very much John. Your code works perfectly.

With your code, when the main index page is closed or if the user browses to another site, the Contact page closes automatically,

whether a message is sent or not, just as I wanted.

Previously in an effort to make everything clearer, I placed an extra opening tag that made it all worse instead. Sorry about that.

Thanks again,

Kenny

jscheuer1
02-25-2016, 11:17 PM
Great! And you're doing the right thing there. You can almost never have too many backups. It's wise to try out new code on a test page. Sadly, sometimes typos creep in that way, which is what I think may have happened here. Worked out in the end. That's what matters.

KennyP
02-26-2016, 12:11 AM
Thanks again John. Your code and where you placed it is purely genius.

This post can definitely be set to resolved.