Page 1 of 1

Check if a window is open

Posted: Tue Jan 30, 2007 5:14 pm
by shiznatix
Ok heres the deal. I have a music page that has a mp3 player on it. Then I have a link to 'detach' the player which just opens the play in a new window then it throws the user to the home page or whatever. The problem is that if that user goes back to the music page while the player is 'detached' then the music player loads again and they both stop working.

So my idea is when the music page is loaded, it checks if the window "MusicPlayer" exists. If it does exist, then I do nothing. Otherwise I open the music player on the page. I have tried such things as:

Code: Select all

if (MusicPlayer && MusicPlayer.open && !MusicPlayer.closed)
{
    alert('win!');//i would NOT include the player
}
else
{
    alert('lose');//i would include the player
}
but that does not work as it just says that MusicPlayer is not set yet in my error console even though that window is open.

Posted: Tue Jan 30, 2007 5:26 pm
by feyd
Last I remember, that's not possible. I'm guessing you did this with Java? There should be a way for you to know if the player is still open through it.

Edit: worst case, you could use a cookie as a flag.

Posted: Tue Jan 30, 2007 5:37 pm
by Burrito
try something like this:

Code: Select all

<html>
<head>
<script>
var newwin = false;
function windowOpen()
{
	newwin = window.open('test.php','nwwin','width=200;height=200');
	newwin.focus()
}

function checkWin()
{
	if(newwin)
		alert('yes');
	else
		alert('no');
}
</script>
<body>
<input type="button" onClick="windowOpen()" value="open"><br>
<input type="button" onClick="checkWin()" value="test it"><br>

</body>
</html>
then on test.php:

Code: Select all

<html>
<head>

</head>
<body onUnload="opener.newwin = false">
blah blah
</body>
</html>

Posted: Tue Jan 30, 2007 5:39 pm
by shiznatix
no i am using a flash program as the music player so the first parts out.

The cookie thing: Can I make a cookie that will only persist while that one window is open? would a session cookie do that or do session cookies persist while the browser is open, even if it was set in a popup?

Edit:

Burr: that would not work because if i left the page then came back while the window was still open then newwin would always be false.

Posted: Tue Jan 30, 2007 5:44 pm
by feyd
Unless I'm thinking of something else, an event is triggered when the window is being closed.

Posted: Tue Jan 30, 2007 6:32 pm
by Burrito
shiznatix wrote:Burr: that would not work because if i left the page then came back while the window was still open then newwin would always be false.
not sure I follow you here...newwin doesn't get set back to false until the window is closed (onUnload). the window losing focus doesn't do anything...did I misinterpret that?

Posted: Wed Jan 31, 2007 9:21 am
by shiznatix
Burrito wrote:
shiznatix wrote:Burr: that would not work because if i left the page then came back while the window was still open then newwin would always be false.
not sure I follow you here...newwin doesn't get set back to false until the window is closed (onUnload). the window losing focus doesn't do anything...did I misinterpret that?
the page that initiates 'newwin' will change. I open the newwin, then on the page that loaded that I go and do a bunch of stuff then somehow link back to the page that can load newwin. Now that I am back, newwin equals null again since I reloaded the page.

Posted: Wed Jan 31, 2007 12:10 pm
by Burrito
ahh I see. you could pass the boolean status of the window to any linked pages via a url param or form var then set it on the load of the next page.