Check if popup window exists by name

I have the name of a potential popup window. I want to check if the window exists.

When I try thename.location.href it throws a js error.

How can I check if a window exists by name?
qwertqAsked:
Who is Participating?
 
ZvonkoSystems architectCommented:
Then use this:

                  var scPlayer = window.open("","scPlayer","top=9000");
                  if(scPlayer.document.body.innerHTML){
                        //alert ('exists, do nothing');
                  } else {
                        scPlayer.close();
                        window.open("scPlayer.html","scPlayer","top=100,height=200,with=400");
                  }


0
 
ZvonkoSystems architectCommented:
Like this:

<script>
var popUp = window.open("","yourPopupWindowName","top=9000");
if(popUp.document.body.innerHTML){
  alert("Exist");
}
</script>


0
 
qwertqAuthor Commented:
I do not think that would work. I am not detecting the window on the same page which opened it. It could be a couple refreshes later.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ZvonkoSystems architectCommented:
It does NOT depend on window, iframe, frameset, frame.
It depends only and solely on window name.

And I do not think it will work, I know it works ;-)

0
 
ZvonkoSystems architectCommented:
Oh, one more thing.
It will only work when both windows, the searching window and the searched window are in the same internet domain.
If you plan to check for popups of another doamin, then check this:
<script>
var popUp = window.open("","yourPopupWindowName","top=9000");
try{
  if(popUp.document.body.innerHTML){
    alert("Exist");
  } else popUp.close();
} catch(e){
    alert("Exist, but not my domain");
}




</script>

0
 
qwertqAuthor Commented:
luckily it is not on an outside site.


In my code I have this:
                  if(scPlayer.document.body.innerHTML){
                        alert ('exists');
                  } else {
                        alert ('does not exist');
                  
                  }

Which errors:
Error: scPlayer is not defined

Also I would like to detect it by window name, not js variable.
0
 
ZvonkoSystems architectCommented:
Check this:


                  var scPlayer = window.open("","scPlayer","top=9000");
                  if(scPlayer.document.body.innerHTML){
                        alert ('exists');
                  } else {
                        scPlayer.close();
                        alert ('does not exist');
                  }

0
 
qwertqAuthor Commented:
I am trying the code before opening the window.
The whole point of this script is to detect if the window has already been opened before trying to open it again.
0
 
qwertqAuthor Commented:
Why are you opening the window at the start of the script?
The window might already exist, I am not trying to make sure it opens.

0
 
ZvonkoSystems architectCommented:
The window.open() method with the empty String as first parameter does NOT load an existing popup again. And that is the trick. By loading an empty URL you get the reference to the window by name without changing its actual state, neither position, size nor URL. Existing window is not really loaded but simple a reference is catched into returning window reference.

When the empty url window open does realy open an empty window, then you know that those popup did not exist before and you need to close first the empty test window and open then your desired popup window.

 
0
 
qwertqAuthor Commented:
Ok I was wondering what that was.
Here is my updated code.

In both FF and IE it does not alert properly when the window has already been opened. I had to set the correct height and width because if i did not when it opened the window it was not the correct size because it was already opened at full screen by the first part.


var width                   = '665';
var height                   = '650';
var window_params       = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left=100,top=50';


var scPlayer = window.open('','scPlayer',window_params);
if(scPlayer.document.body.innerHTML){
      alert ('exists');
} else {
      var url             = '/videos/Player';
      var url_params       = '';
      
      window.open(url+url_params, 'scPlayer', window_params);
}
0
 
ZvonkoSystems architectCommented:
One question: if you refresh the window with upper code that you say "does not alert properly", do you get then two windows opened? If not, then is your window name spelling WRONG. it is something else bot not this: 'scPlayer'





0
 
ZvonkoSystems architectCommented:
Perhaps you do not know how a window name can be set.
Where is your original popup window name set and how?

0
 
qwertqAuthor Commented:
Whoops I had forgotten I was re-setting the window name inside the actual html with a window.name=
I removed this and it is working correctly now.

I am setting the window name herE:
 window.open(url+url_params, 'scPlayer', window_params);
is this correct?
0
 
ZvonkoSystems architectCommented:
Yeap! That is one of three methods.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.