Check if a window is open

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Check if a window is open

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless I'm thinking of something else, an event is triggered when the window is being closed.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Post Reply