Solved

Check if window is open

Posted on 2004-10-01
34
7,476 Views
Last Modified: 2009-07-03
The same pop-up window can potentially be opened from multiple places.  Is there any way to check if it is already open?

For example:
The three files are fileA.asp, fileB.asp, and popup.asp.
fileA.asp causes popup.asp to open.
While popup.asp is still open, fileB.asp causes popup.asp to also open.
Before popup.asp opens for a second time, resulting in two popup.asp windows open, is there a way to check if it already exists?
0
Comment
Question by:ccboyd
  • 12
  • 12
  • 6
  • +3
34 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12203557
Hi ccboyd,

Yes, this is possible. Make sure both window.open uses the same name, as in

window.open(url,'sameNameForBoth');

Then, you can test

if (sameNameForBoth||sameNameForBoth.closed) {window.open(url,'sameNameForBoth');}

Regards,
Zyloch
0
 

Author Comment

by:ccboyd
ID: 12203739
Zyloch,

I tried your suggestion above and ran into some probelms.

I currently have the following in fileA.asp and fileB.asp:
if ("Validation" || "Validation".closed)    //if i don't put these here, i get a "'Validation' undefined" error
{
      window.open(url, "Validation");
}
else
{
      alert("window exists");
}

The first window opens fine, but when the second window attempts to open it does show me the "window exists" alert.  Instead, it replaces the first popup.asp with the second popup.asp.

0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12203762
Try doing

var Validation = window.open(url, "Validation");
0
 

Author Comment

by:ccboyd
ID: 12203908
Now i get:

'Validation.closed' is null or not an object
0
 
LVL 9

Expert Comment

by:riyasjef
ID: 12204020
try this

if(Validation)
if(!Validation.closed)
   {
      // code

  }

Riyasjef
0
 
LVL 25

Expert Comment

by:devic
ID: 12204631
here is howto:
========================
<script>
function checkWindow(myName)
{
      try{if(eval("window."+myName+".document"))return true;}catch(e){}
      return false;
}

function popUp(theUrl, myName)
{
      eval(myName+" = window.open(theUrl, myName)");
}
</script>
<button onclick="popUp('about:blank','mypopup')">popUp</button>
<button onclick="alert(checkWindow('mypopup'))">checkWindow</button>
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12205332
You can check very simply if a window has been opened and closed or has never been opened

------------
if (typeof(newWindow) == "undefined" || newWindow.closed == true)  {
    // The window is closed
}
else  {
    // The window is already open
}
-------------
I use this code for a gallery page on a site

C
0
 

Author Comment

by:ccboyd
ID: 12217449
I think the solutions posted would work if the same file that was opening the new window was then also checking if the window was already opened, but that is not the case.  

The solutions above are not working with the scenario I currently have.  

Both fileA.asp and fileB.asp contain the same code below and allow the window to open every time.

if (typeof(win) == "undefined" || win.closed == true)  
{  
    // The window is closed
    var win = window.open(url, "win", "left=50, top=50, height=400, width=600, resizable=yes, scrollbars=yes");
}
else  
{  
    // The window is already open
    alert("window exists");
}
0
 
LVL 25

Expert Comment

by:devic
ID: 12217489
ccboyd check my example ;)
0
 

Author Comment

by:ccboyd
ID: 12217726
devic,

With your example, I get the error
'window.win.document' is null or not an object
0
 
LVL 25

Expert Comment

by:devic
ID: 12217750
it would be nice, if you upload the example online and give us a link
0
 

Author Comment

by:ccboyd
ID: 12217845
Unfortunately, no link is avialable.

The following can be found within fileA.asp and fileB.asp.
Only the values in the very first if statement will be different.

<script>
if ("<%=m_inContacts%>" != "&nbsp;" || "<%=m_curSchedules%>" != "&nbsp;" || "<%=m_inConGroups%>" != "&nbsp;")
{
    var url = "m_validation.asp?s_Contacts=" + "<%=m_inContacts%>" + "&s_curSchedules=" + "<%=m_curSchedules%>" + "&s_ConGroups=" + "<%=m_inConGroups%>" + "&nGroupID=" + "<%=m_nGroupID%>"
            
    var myName = "win";
    if(eval("window."+myName+".document"))
    {
        alert("window exists");
    }
    else
    {
        var win = window.open("m_validationframes.asp?url=" + url, "win", "left=50, top=50, height=600, width=1000, resizable=yes, scrollbars=yes");
        if(window.focus())
        {
            win.focus()
        }
    }
}
</script>
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12217903
I would not recommend naming your window and the object variable the same - this could cause problems and could get confusing.

varWindow = window.Open(...)

...

if (typeof(varWindow) == "undefined" || win.closed == true)  . . .

ALso, remove all the spaces from you window options...

var win = window.open(url, "win", "left=50,top=50,height=400,width=600,resizable=yes,scrollbars=yes");

Although I am pretty ceratin this will not solve your current problem it will prevent cross-browser issues in future.  I do not know the reason behind this though.  it is something I have come across before.

C
0
 
LVL 25

Expert Comment

by:devic
ID: 12217933
ccboyd, are you sure that this is my example?

tip: I use some try, catch ;)
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12217995
Another options may be this.

When you need to load a new page into your window( from any main page), use the open method again with the same window name.  If the window is closed it will open it, if the window is open it will change the contents.

Ensure you window name is always the same and you should be OK.  

C
0
 

Author Comment

by:ccboyd
ID: 12218249
devic -
Yes, you're right.  I did not use your try/catch lines.  I did just add them in and they are just now catching the "... null or not an object" error mentioned earlier.

snavebelac -
Thanks for the suggestion concerning the spaces in the window options.  Fortunately, based on client requirements, browser cross-compatibility is not an issue.
Also, let me explain in a little more detail as to why I want to check if the popup window is already open.  The popup window that is opened initially should contain a listing of functional validation reasons as to why certain data on a form cannot be saved.  (for example, an inactive contact cannot be assigned to an active Group)  If this popup window is kept open and other actions within the parent window cause another popup functional validation window, then that is when I want a check so that window does not open and other actions are taken (like a confirmation, etc.).
So I would not want the contents of the original window to change automatically.
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12218536
I think I understand.  Perhaps I did not explain myslef clearly the first time.

Otherwise I think what I said is still valid.  if you use window.open with the same window name it will replace the content in the open window.  If the window is not open it will open it.  Is this not what you want to achive.  It will only load content into the window in response to a window.open command.

I almost sounds as if you are simply conveying a message to the user about some action they are performing.  Can you just use an alert or confirm box instead ?

Perhaps this link will help  http://tech.irt.org/articles/js205/

C
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:ccboyd
ID: 12219551
snavebelac-

No, I do NOT want to replace the original content of the popup window with new content.  That original content is very important and should not be replaced without the user's consent.  With that said, you can understand why I would like to first check if the popup window is already open.  If it is not, I would like to conitnue with the window.open command, but if it does exist then I would like to do other actions.

Yes, I am in essence conveying a message based on actions that the user is performing. However, the message is not so simple that it should be included in an alert or a confirmation.  The poup window will show them numerous specific functional reasons as to why their data was not saved and the opportunity to go into a "quick fix" mode where steps can be taken to save the data.
0
 

Author Comment

by:ccboyd
ID: 12219947
Does anyone have any more suggestions?

I am beginning to think that a problem might be that the object variable that opens the popup cannot be referenced from another file.  I have attempted to make this a global variable, but then it seems as if its properties are not detected by the second file.

Any help would be greatly appreciated.
Thanks.
0
 
LVL 25

Expert Comment

by:devic
ID: 12220042
>>>Yes, you're right.  I did not use your try/catch lines.  I did just ....

ccboyd, if you do, as I wrote, then everything will be ok.  What do you wait for?
0
 

Author Comment

by:ccboyd
ID: 12220281
hi devic -

My full statement earlier was "Yes, you're right.  I did not use your try/catch lines.  I did just add them in and they are just now catching the "... null or not an object" error mentioned earlier."

I tried it one more time to be sure and the try/catch is still catching an error:
'window.NewWindow1.document' is null or not an object

<code>

try
{
      var myName = "NewWindow1";
      if(eval("window."+myName+".document"))
      {
            alert("window exists");
      }
      else
      {
            var win = window.open(url, "NewWindow2", "left=50, top=50, height=400, width=600, resizable=yes, scrollbars=yes");
      }
}
catch(e)
{
      alert(e.name);
      alert(e.message);
}
            
if(window.focus())
{
      win.focus()
}

</code>


Not sure if you saw my statement earlier, but I am starting to think that the second asp file is not detecting variables or window names set in the first file.

By the way, I really do appreciate you checking up with this question.
0
 
LVL 25

Expert Comment

by:devic
ID: 12220427
ccboyd np,
this is interesting question and I know only one 100% method to check if the window is open. To check his document.  For this reason we need try, catch.

 >>>I did just add them in and they are just now catching the "... null or not an object" error mentioned earlier."

exactly, we try to access the document, and we can access it ONLY if the window really exists.
try
{
    // here we try to check document
}
catch(e)
{
    // means, that there NO window
    // then here create new window
}

so here we have  try, catch  not for debug purposes, but as part of code.

that's all.
0
 
LVL 25

Assisted Solution

by:devic
devic earned 50 total points
ID: 12220583
and here is description of my function above:
======================
function checkWindow(myName)
{
    try
    {
         if(eval("window."+myName+".document"))/* here I try to access document. If I can, means window exists.*/
         {
                return true;  /*fyi, return means exit function right now*/
         }
         
    }
    catch(e)
    {
        return false;/*But if we don't have the window above, occurs error, we catch it, and return false */
    }
}
0
 

Author Comment

by:ccboyd
ID: 12220653
I see what you are saying now.  Unfortunately, it catches an error both when a window exists and when it doesn't.  :(

(i was so excited there for a minute)
0
 
LVL 25

Expert Comment

by:devic
ID: 12220804
0
 
LVL 25

Expert Comment

by:devic
ID: 12220820
0
 
LVL 25

Expert Comment

by:devic
ID: 12220866
tested on:
IE 6.0
mozilla 1.6
firefox 0.8
netscape 7.1
opera 7.54

everywhere works :)
0
 

Author Comment

by:ccboyd
ID: 12220967
Yes, I completely see how that works.

However, mine is completely different.  The check to see if that popup exists is not on the same page of code where the popup is originally opened.  

For example:
On this example
http://home.arcor.de/athens/expexc/js/window/check_if_window_exists/check_if_exists_window_short.html
If there was another window open other than the child or the parent that could produce another popup, can you check if the original popup is open then?
0
 
LVL 25

Expert Comment

by:devic
ID: 12221037
I got it. No with my way.
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12221462
Basically want you want is a way to reference a popup from a window other than the one that opened it ?  If this is not the case then I don't think I understand

C
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 75 total points
ID: 12221611
Why not have the message in a cookie for example (4K max) and dump the cookie content in the window whenever it is referenced, opened or whatever.

w=window.open('cookiedumper.html','cookiewindow')


and in cookiedumper.html simply write the formatted contents of the cookie and focus itself

Michel
0
 
LVL 6

Expert Comment

by:snavebelac
ID: 12221723
If I understand correclty then there is no way to do what you want.

I don't think it is possible to reference a namned window from anything other than itslef or its opener.  A page in completely separate document will not have access to the objects present in the other widows.

I could be wrong and I will continue to investigate but I think this is the case.

Perhaps if you describe what you are trying to do in general terms someone will be able to come up with an alternative solution.

C
0
 

Author Comment

by:ccboyd
ID: 12227081
Thank you so much to everyone for your help!

I think I am going to take mplungjan advice and use cookies with this scenario.

However, I went ahead and split the points between him and devic.  devic spent a lot of time attempting to solve my issue and helped me see that what I originally wanted to do is impossible.

Thank you again.
0
 
LVL 25

Expert Comment

by:devic
ID: 12232142
ccboyd,

yes, mplungjan's idea is good choise

thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now