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

Checking if a window exists?

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Hi,
I've got a music player on my site that launches in a new window. I don't want to display the "launch music" link if the window is already open and playing. This problem is driving me nuts... Here is where I'm up to:

<script type='text/javascript'>
if(!new_win) {
var new_win;
}

function variable_in_link(mem_id)
{
var new_win = window.open('}

if(!new_win) {
// display my "open music player" link
}

The music player link is displayed all the time. Any ideas why I'm not detecting that the window is open?

I've tried:
if (new_win == "" || new_win.closed || new_win.name == undefined)
But I just get an error saying that new_win is a null object...

Any help would be really appreciated.

Many thanks

John ;-)
</script>

I don't make mistakes, I'm merely beta-testing life.
 
But I just get an error saying that new_win is a null object...

Are you sure that's what the error says? Perhaps it says it's "null or not an object"?

One thing to check is where you testing the variable. If it's outside your function, then it would be null or not an object, because you're defining the variable local to the scope of that function.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi Dan,
Thanks for your tip. I did some checking on Google re: global variables and changed the code but unfortunately it still isn't working. Here's the revised code and I've put in some comments to show how I think it should be working...

if(!new_win) { // IF NEW_WIN DOESN'T EXIST DEFINE IT (IS THIS NOT A GLOBAL VAR NOW?????)
var new_win = null;
}
function variable_in_link(mem_id)
{
// ASSIGN THE WINDOW TO NEW_WIN - IS THIS GLOBAL????
new_win = window.open('}



<script type='text/javascript'>
if(new_win) { // CHECK GLOBAL NEW_WIN TO SEE IF WINDOW OPEN
if (new_win == "" || new_win.closed || new_win.name == undefined) {
// SHOW THE LINK FOR THE MUSIC WINDOW - REMOVED FOR CLARITY
}
} else {
// SHOW THE LINK FOR THE MUSIC WINDOW - REMOVED FOR CLARITY
}
</script>

I don't make mistakes, I'm merely beta-testing life.
 
ADDITION...

I've added the code that calls the function below just in case that's where problem lies:

var a = '<div id="showmusiclink"><a href="javascript:void(0);" title="Click here to play music" onClick="javascript: variable_in_link(&#39;<?php echo $mem->ID; ?>&#39;);">show music</a></div>';
var total = a;
document.write (total);

I don't make mistakes, I'm merely beta-testing life.
 
Just had another thought...

Am I right in assuming that once new_win has been assigned and the window open then the var stays assigned when they click a different page on my site? Or will the var be reset?

Is there a way of putting the information that the window is open in a $_SESSION variable or something ?

???

I don't make mistakes, I'm merely beta-testing life.
 
The variable will be reset. However, if the popup window is still open, attempting to re-open it will re-populate the variable with a handle to the popup without opening a second window.

This is the case because you have specified a window name in the second parameter to the window.open call.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Dan

Thanks so much for all your help, it is really appreciated. I've approached the problem from a different angle now and managed to get it working how I want.

In case anyone else is reading this with the same problem, here is how I did it.

I made the window call itself in the <a href and appended "?music=on" to the url.

Then in the script I had:
if($_REQUEST['music'] == "on") {
$_SESSION['musicon'] = $ID;
}

Then I just check to see if the musicon Session var is equal to the current ID of the page and it doesn't display the Music On link if it is.

Then in the new window I have a Stop Music button that calls itself with "music=off" and that window contains the following code:

<?php if($_REQUEST['music'] == "off") {
unset($_SESSION['musicon']);
?>
<script type="text/javascript">
new_win = top;
window.opener='new_win';
window.close();
</script>
<?php }

Which unsets the Session Var and closes the music window (unless your in FF and then it doesn't close the window.. but that's another story..)

Thanks again Dan for all your help.

Cheers

John ;-)




I don't make mistakes, I'm merely beta-testing life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top