Go Premium for a chance to win a PS4. Enter to Win


How to detect previously opened window in Javascript

Posted on 2003-12-01
Medium Priority
Last Modified: 2011-09-20
context: IE only (5+):

On one of my web pages (say, "foo.html") , I open a new side window using "window.open('bar.html', 'sidebar')".  The user may then leave foo.html.  However, when she returns to foo.html, I want to open the side window again but only if it is or has been closed.  If it's still open, then I do not want the sidebar window touched because it may have been changed by the user.

Is there a way to detect whether this sidebar window is open or not?

Question by:pbrooks
  • 3
  • 3
  • 2
LVL 25

Assisted Solution

devic earned 400 total points
ID: 9853646
hi pbrooks,

here is an example:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--// created by devic :oldlook.experts-exchange.com
function popUp(url, myname, w, h, scroll) {
     var winl = (screen.width - w) / 2;
var wint = ((screen.height - h) / 2)-20;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=0, status=0, resizable=0,menubar=0'
eval(myname+" = window.open(url, myname, winprops)");
if (parseInt(navigator.appVersion) >= 4) { eval(myname+".window.focus()"); }

function checkWindow(namez)
                  alert("Window \""+namez+"\" open");
                  alert("Window \""+namez+"\" not found");
            alert("Window \""+namez+"\" not found");


<a href="somePage.htm" onClick="popUp('','test1','295','300','no');return false;">Test1</a>
<br />
<a href="somePage.htm" onClick="popUp('','test2','295','300','no');return false;">Test2</a>
<p />
<input type="submit" value="test window test1" onClick="checkWindow('test1');" />
<input type="submit" value="test window test2" onClick="checkWindow('test2');" />

Author Comment

ID: 9853902
Thanks, devic, but unfortunately this does not work for me.  Your "checkWindow()" only works if I stay on your page, but not if I leave your page and then return to it (using, say, the browser's Back button or hyperlink).


Assume that you added another link on your page above, taking the user to a new page called "b.html".  If the user:
1. clicks on your popup('','test1') to create the "test1" side window
2. clicks on checkWindow('test1') -- this works.
3. leave this page, going to "b.html" via the new link
4. returns to this page using the Back button
5. now, checkWindow('test1') does not work

LVL 25

Expert Comment

ID: 9853961
ok, then i think better to use cookies.

on your window popup onunload save cookie popup=closed;

and in every page you can ask for this cookie.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 9855057
While cookies are an interesting solution, they will not work in my case.  My popup window is a login page to an entire site of hundreds of pages, and I cannot modify all of them to deal with the cookie.
LVL 10

Accepted Solution

NetGroove earned 1600 total points
ID: 9856231
Here is my proposal.

You have to know that all windows can be accessed by window name.
The window name is the second parameter in window.open() method, in your case is the concerened window name: 'sidebar'

Ok, now, opening a new window with the same name will NOT open a new window, it will reuse the already opened window.

You can write it like this:
 nw = window.open('', 'sidebar');

Now you have to know is this your old window or is it a new window.
The new window would be a blank window withouth any code and without private variables.
So you could check for the old variables which are present in 'bar.html'.
If the regualr variables from 'bar.html' are not there, then this is an empty window.
Check it like this:

 nw = window.open('', 'sidebar');
    nw = window.open('bar.html', 'sidebar');

If there is no global var which you could reuse in the uper test, then you could assign a new globaö var specialy for that test.
Like this:

 nw = window.open('', 'sidebar');
    nw = window.open('bar.html', 'sidebar');
    nw.myGlobalVar = 'myData';

The first time when you open your bar.html you add a var of your choice to that window and check at reopen time for the existance of that var.

Do you need a complette example for this or is the explanation clear enough?


Author Comment

ID: 9859818
The problem with creating a variable in the side window is that the user may use the side window to navigate to lots of other pages inside the web site, and I expect that the variable will be lost when she activates a hyperlink.

However, I have discovered something similar: you can try to get a property of the side window after opening a blank document (as per your suggestion of window.open('', 'sidebar')).  If you fail to read the property, then a window by that name is already open, but if you succeed, then you've successfully opened a blank window and can close it and open the real one...

function OpenSidebarIfNotAlreadyOpen() {
var wBar, sleft;

// try to open a blank window with the name: 'sidebar'

wBar = window.open('', 'sidebar');

// now try to read a property (let's say, 'screenLeft')

try {
sleft = wBar.screenLeft;

// if we succeeded then we opened a blank window
//    and should close it and open the real one...


catch(e) { // if we failed to read the screenLeft property, then sidebar is already
// open and we don't need t odo anything


LVL 10

Expert Comment

ID: 9859950
Thanks for sharing your experience with us.

But please take note that grading an answer with a B is unnecessary waste of expert points.
It is a win for you and for experts if you grade the comments with an A.
You can correct even after grading your last grading, if you want.
Post simply a grade correction request in this topic area:

And give B only if you are disappointed after several requests for clarification, never give a C, better let delete a question before giving a C.

Thanks in advance,

LVL 10

Expert Comment

ID: 9862607
Thanks C101.

Thank you pbrooks.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
Suggested Courses

886 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