Solved

Check if popup window exists by name

Posted on 2007-03-19
15
433 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:qwertq
  • 9
  • 6
15 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18748824
Like this:

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


0
 

Author Comment

by:qwertq
ID: 18748855
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18748902
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749008
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
 

Author Comment

by:qwertq
ID: 18749228
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749286
Check this:


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

0
 

Author Comment

by:qwertq
ID: 18749350
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
ID: 18749374
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
 

Author Comment

by:qwertq
ID: 18749456
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749493
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
 

Author Comment

by:qwertq
ID: 18749691
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749769
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749782
Perhaps you do not know how a window name can be set.
Where is your original popup window name set and how?

0
 

Author Comment

by:qwertq
ID: 18749859
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749887
Yeap! That is one of three methods.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now