View Full Version : cannot close popup

07-13-2011, 05:09 AM
I am having a heck of a time closing this popup window when i logoff the site.

i open it by

shoutwindow = window.open("<?=$MY_LINK_ROOT?>/shoutbox/minichat.php", "shoutwindow", "location=0,status=0,scrollbars=0,menubar=0,resizable=0 width=300,height=310");

when i click logoff the site i want the popup to close also

here is the logoff php


//only for testing will remove this



echo "window open";

//shoutwindow.close (); // also tried both these didnt work
// window.close();




echo "Window already closed";



// Unset all of the session variables.


// Finally, destroy the session.


echo "<script> location.href='$MY_LINK_ROOT'</script>";



what am i missing here lol ? thanks

UPDATE: i just had an idea, can i mix a href and an onClick together so that both are executed like this (i never tried it before)

the reason i use self.close is because i heard that it always closes the child not the parent.

<a href="logoff.php" onClick="self.close()" return:true;>Log Off</a>

the return true will make it execute the href as well right?

Old Pedant
07-13-2011, 05:23 AM
Ummm...the close code has to be in *JAVASCRIPT*. You can't close windows from PHP code.

But more than that, only the page that *OPENED* the window can close it [there are ways around this...they are a pain in the patootie], so clearly a subsequent PHP page can't close what a prior PHP page opened.

(The reason for that latter restriction: You must have a JavaScript object reference to the other window. You get that by doing var w = window.open(....); so that later you can do w.close( ). But how do you pass that JavaScript object reference from one incarnation of a window to another??? The general answer: you don't. The hacky trick is that you *can* try to REOPEN the same window--by NAME--and if it's already open you get a reference to the already open window instead of opening another window. But what if the window isn't open? Now you have opened it when you didn't want to. Anyway... you sure can't do any of it from PHP code.)

07-13-2011, 05:28 AM
ohhhhhhhh dang ok thanks bud appreciate that very much.

Old Pedant
07-13-2011, 05:37 AM
See my added (edit) last paragraph in prior post, too.

It *can* be done if you are careful.

Old Pedant
07-13-2011, 05:38 AM
And self is just a synonym for "this window". So it closes whatever window calls it.

07-13-2011, 05:48 AM
ok thanks it wont be that hard to convert it to javascript but its a trivial thing i wanted to do, just for asthetics is all. Prob not worth the time, without a session they cant post in the shout anyway so its just a dead popup sitting in their tray is all and they will eventually close it.

thanks so much for the explaination for that.


Old Pedant
07-13-2011, 06:03 AM
You know, what you *could* do is have the popup monitor its own activity (I assume it is using AJAX?) and, if there isn't any activity for XX minutes, it could then turn itself off. Or ask the user if he/she wants it turned off.