PDA

View Full Version : Checking on open windows


otrsteve
08-11-2002, 07:52 PM
Is there a way to check if a particular window is open, and if it is, reference it by not using the traditional:
newwindow = window.open("url", "name", "features")

I am trying to write to a textarea on a form, by using another window that changes it's content each time I do a search. By clicking on a button from this window I add that new text to the textarea on the orginal window/form. The problem is every time I refresh the window with my new search results, it reopens the window with the form, thus losing the previous content in the textarea. The only way I know of to reference the window object is with the window.open() method.

Here is the code I am currently using:

var OrderWindow = window.open("","OrderFormWindow");
window.focus();

function orderWindow()
{

var URL = "OrderForm.html";
var Hi = screen.height - 350;
var Name = "OrderFormWindow";
var WiHi = "width=430,height="+Hi;
Features = WiHi + ",left=700,top=0,scrollbars,resizable";
OrderWindow = window.open(URL,Name,Features);
window.focus();
}



function AddToItemOrderForm(NewItem)
{
var oldItem;
var newItem;
var NewItem;
//alert(NewItem);
oldItem = OrderWindow.document.OrderForm.itemsOrdered.value;
//alert(oldItem);
newItem = oldItem +"\n" + NewItem;
OrderWindow.document.OrderForm.itemsOrdered.value = newItem;
return;
}



Does anyone know of another method to determine if a particular window is open and referencing it's window object without reopening it? Any help or suggestions would be most appreciated. Also, if it is impossible, I'd like to know that too so I can explore a different approach to solving my problem.

Thanks for any help you can offer.

A1ien51
08-12-2002, 02:52 AM
I wrote a page on this

http://www10.brinkster.com/a1ien51/Scripts/PopUpCheck.htm

also you may want to try to replace

var OrderWindow = window.open("","OrderFormWindow");

to

var OrderWindow = window.open("","OrderWindow");

see what works for you

A1ien51

otrsteve
08-12-2002, 07:44 PM
A1ien51,

Thanks for the help. Unfortunately it doesn't solve my particular problem. In my situation, I am using a Perl script to search a database, and then writing the results to a frame. The results of the search have the option to be automatically written to the textarea contained in another window, via the javascript routine in my original post. Because each search reinitializes the frame, the information contained in the OrderWindow reference is lost. The original window has to be opened again to regain the reference. In Netscape, if the window is already opened, it is left alone, so the new information can be added to what already is there. But in IE the window is always recreated, thus losing the previous information. If there was another way to reference the window other than the window.open() method, that would prevent the window from being rewritten.

I tried using a cookie to save the OrderWindow = window.open() reference, but it didn't work (or I didn't execute it properly). I am trying not to use cookies at all to accomplish this, but it's beginning to look like a lost cause.

Any other suggestions would be appreciated.

adios
08-13-2002, 01:14 AM
If you call window.open() and use a name (second argument) that matches an already existing window, the function simply acts as a document loader; in addition, the variable you return it to picks up the reference to the window (object) again.

otrsteve
08-13-2002, 05:58 PM
Adios, here is the code I am using to reference the window:

var OrderWindow = window.open("","OrderFormWindow");

In Netscape, this behaves as you stated, that it works as a document loader and returns the reference. By leaving off the URL it just references the window, thus allowing me to add to the information already present in the textarea of the form each time I reload the original opener window.

But, in IE, when the window.open code is executed in a subsequent reloading of the parent window, IE rewrites the child window to a blank window titled "about: blank". Do you know of any workaround for this behavior in IE?

Also, since each browser keeps an active list of windows open in their respective menu bars, is there anyway to access this information with javascript, or possibly another language that could be activated from javascript? I know I'm grasping at straws, but I'd really like to accomplish this client side, in real time, and not have to resort to server side methods and the inherent delays in redrawing windows.

Maybe you can explain, or point me to a reference, regarding the actual open.window() reference code passed to the variable. When I used an alert to print out the var OrderWindow it was [window object]. When I used code to save this as a cookie, and then recall it on the next reload, the OrderWindow variable was still [window object], but it no longer worked as a reference, and I would get an error of "window.document has no properties." I can't find any documentation with the few books I have that goes deeper into what is actually happening.

I appreciate your help.

adios
08-13-2002, 06:48 PM
otrsteve...

No comment on that business about the straws.:confused:

Otherwise: MSIE should behave as NS with no url specified in .open(). Maybe you might post the exact fragment in context; could be a detail.

Checking windows (ActiveX again):

http://www.faqts.com/knowledge_base/view.phtml/aid/2248/fid/124

Since printing (alert()s, e.g.), cookies, etc., are string-based, if you try to output objects, the runtime engine will (helpfully) run the object's .toString() method to supply something useful. [object Window] is a good example - useless, of course, except as a one-time indicator of what sort of data is being displayed.

http://caucuscare.com/~roth/JAVASCRIPT/ch09_01.htm

otrsteve
08-13-2002, 09:49 PM
Adios

I couldn't use the first reference you listed, as I need to have this work on both Windows and Mac OS. I'll read the other link later or go buy the book, as I don't have it in my library. In the meantime, taking your suggestion of providing the rest of the code, I'm including the code for both documents here. It's not that long. If you would be so kind, you can cut and paste both parts into seperate html documents and test it in each browser. I've submitted each part in a seperate reply, since they both wouldn'd fit together. It shouldn't take more than 5 minutes. Remember to save the test page as "TestWindow.html".

Thank you.

----------------------

##################### MAIN PAGE HTML #####################


<HTML><HEAD><TITLE>SearchResultsNoCookie</TITLE>
<script>

// Initialize variables

var URL = "TestWindow.html";
var Hi = screen.height - 350;
var Name = "OrderFormWindow";
var WiHi = "width=430,height="+Hi;
var Features = WiHi + ",left=700,top=0,scrollbars,resizable";

// Determine if browser is Netscape or Explorer. If Explorer then
// move the new window to the top right of the screen.

var isIE = 0;
if (navigator.appName.indexOf('Microsoft Internet Explorer') != -1)
{
isIE = 1;
}

if (isIE != 1)
{
alert("Netscape");
var OrderWindow = window.open("","OrderFormWindow");
alert(OrderWindow);
window.focus();
}
else
{
alert("Explorer");
var OrderWindow = window.open("","OrderFormWindow");
newWindow = OrderWindow.window.self;
newWindow = newWindow.open("","OrderFormWindow");
newWindow.focus();
newWindow.resizeTo(400,300);
newWindow.moveTo(700,0);
alert(OrderWindow + "\n" + newWindow);
}


// If no window is present, the call this function to open
// the new window. If the browser is Explorer then use different
// code to move it to the top right corner.

function orderWindow()
{
OrderWindow = window.open(URL,Name,Features);
if (isIE)
{
var newWindow;
newWindow = OrderWindow.window.self;
newWindow.resizeTo(400,300);
newWindow.moveTo(700,0);
newWindow.scrollTo(0,600);
//newWindow.window.scrollTop = 350;

}
else
{
OrderWindow.window.scroll(0,600);
}
window.focus();

}


// This function writes the text to the textarea in the other
// window adding each line to the previous one.

function AddToItemOrderForm(NewItem)
{
var oldItem;
var newItem;
var NewItem;
//alert(NewItem);
oldItem = OrderWindow.document.OrderForm.itemsOrdered.value;
//alert(oldItem);
newItem = oldItem +"\n" + NewItem;
OrderWindow.document.OrderForm.itemsOrdered.value = newItem;
return;
}


</script>



<!-- The actual HTML for the page is generated by a Perl script doing a
search on a database and writing the matches so that the user has the
option of adding the information to the seperate window by clicking a button. -->

</HEAD><BODY bgcolor = #ffffff>

<form><p><b><input type="button" name="orderWindowbutton" value=" SPECIAL ORDER FORM " onClick="orderWindow()"> </b></p></form>

<SPAN CLASS='title'>C-230: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Dagwood Becomes Mr. Dithers 12/3/44 (Arthur Lake, Penny Singleton) (Excellent, Disc/AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Alexander, The Actor , ../../50 (Hanley Stafford, Arthur Lake) (Excellent, Network, Comedy) Cassette Length: 60 Min.</SPAN><form name="0"> <input type="button" name="AddItem" value="Add C-230: BLONDIE" onClick="AddToItemOrderForm('C-230: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-500: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>The New Car 11/17/48 (Arthur Lake, Penney Singleton) (Excellent, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Paris, France (Hanley Stafford, Ann Rutherford) (Scratchy, Disc/AFRS, Comedy) Cassette Length: 60 Min.</SPAN><form name="1"> <input type="button" name="AddItem" value="Add C-500: BLONDIE" onClick="AddToItemOrderForm('C-500: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-505: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Borrowing Dagwood's Tools 10/15/44 (Penny Singleton, Arthur Lake) (Excellent, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Alexander's Newspaper 11/3/48 (Hanley Stafford, Arthur Lake) (Excellent, AFRS, Comedy) Cassette Length: 60 Min.</SPAN><form name="2"> <input type="button" name="AddItem" value="Add C-505: BLONDIE" onClick="AddToItemOrderForm('C-505: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-693: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Dagwood Loses Mr. Dither's $5,000 1/6/52 (Ann Rutherford, Arthur Lake) (Very Good, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>A Skeik Wants Blondie For His Harum 1/13/52 (Ann Rutherford, Hans Conried) (Very Good, AFRS, Comedy) Cassette Length: 60 Min.</SPAN><form name="3"> <input type="button" name="AddItem" value="Add C-693: BLONDIE" onClick="AddToItemOrderForm('C-693: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-935: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Baby Snooks Visits The Bumsteads 10/8/44 (Arthur Lake, Penny Singleton) (Excellent, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>The Bumsteads Plan Their Vacation 7/27/47 (Penny Singleton, Arthur Lake) (Very Good, Network, Comedy) * 7-27-47 is slightly distored during last 5 minutes. Cassette Length: 60 Min.</SPAN><form name="4"> <input type="button" name="AddItem" value="Add C-935: BLONDIE" onClick="AddToItemOrderForm('C-935: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-1122: BLONDIE</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Dagwood's Icy Challenge 6/18/44 (Arthur Lake, Penny Singleton) (Excellent, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Blondie Becomes Aunt Poly Of Cupid's Question Column (Hanley Stafford, Frank Nelson) (Very Good, AFRS, Comedy) * Both programs transferred digitally. Cassette Length: 60 Min.</SPAN><form name="5"> <input type="button" name="AddItem" value="Add C-1122: BLONDIE" onClick="AddToItemOrderForm('C-1122: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>CD-317: BLONDIE</SPAN><br /><SPAN CLASS='number'> Track 1 - </SPAN><SPAN CLASS='normal'>Dagwood's Icy Challenge 6/18/44 (Arthur Lake, Penny Singleton) (Excellent, AFRS, Comedy)</SPAN><br /><SPAN CLASS='number'> Track 2 - </SPAN><SPAN CLASS='normal'>Blondie Becomes Aunt Poly Of Cupid's Question Column (Hanley Stafford, Frank Nelson) (Very Good, AFRS, Comedy) Compact Disc Audio Length: 60 Min.</SPAN><form name="6"> <input type="button" name="AddItem" value="Add CD-317: BLONDIE" onClick="AddToItemOrderForm('CD-317: BLONDIE')"></form><br /></P>
<SPAN CLASS='title'>C-1102: COMIC WEEKLY MAN</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Beetle Bailey, Prince Valliant, Roy Rogers, , Flash Gordon, Robin Hood 12/7/52 (Lon Clark) (Excellent, No Commercials, Children's Show)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Rusty Riley, Little Iodine, Dick's Adventures, , Blondie, Flash Gordon 12/14/52 (Lon Clark) (Excellent, No Commercials, Children's Show) * Both programs transferred digitally. Cassette Length: 60 Min.</SPAN><form name="7"> <input type="button" name="AddItem" value="Add C-1102: COMIC WEEKLY MAN" onClick="AddToItemOrderForm('C-1102: COMIC WEEKLY MAN')"></form><br /></P>
<SPAN CLASS='title'>C-1104: COMIC WEEKLY MAN</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Comic Weekly Man Reads The Sunday Comics For 3/30/52 , Hopalong Cassidy, Prince Valiant, Uncle Remus , Flash Gordon, Dick's Adventures (Lon Clark) (Very Good, No Commercials, Children's Show)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Comic Weekly Man Reads The Sunday Comics For 4/6/52 , Rusty Riley, Blondie, Roy Rogers, Donald Duck (Lon Clark) (Very Good, No Commercials, Children's Show) * Both programs transferred digitally. Cassette Length: 60 Min.</SPAN><form name="8"> <input type="button" name="AddItem" value="Add C-1104: COMIC WEEKLY MAN" onClick="AddToItemOrderForm('C-1104: COMIC WEEKLY MAN')"></form><br /></P>
<SPAN CLASS='title'>C-1108: COMIC WEEKLY MAN</SPAN><br /><SPAN CLASS='number'> Side 1- </SPAN><SPAN CLASS='normal'>Comic Weekly Man Reads The Sunday Comics For 10/12/52 , Beetle Bailey, Roy Rogers, Flash Gordon, Robin Hood, , Donald Duck (Lon Clark) (Excellent, No Commercials, Children's Show)</SPAN><br /><SPAN CLASS='number'> Side 2- </SPAN><SPAN CLASS='normal'>Comic Weekly Man Reads The Sunday Comics For 10/19/52 , Dick's Adventures, Blondie, Prince Valiant, , Rusty Riley (Lon Clark) (Excellent, No Commercials, Children's Show) * Both programs transferred digitally. Cassette Length: 60 Min.</SPAN><form name="9"> <input type="button" name="AddItem" value="Add C-1108: COMIC WEEKLY MAN" onClick="AddToItemOrderForm('C-1108: COMIC WEEKLY MAN')"></form><br /></P>
<P><CENTER><HR><SPAN CLASS='Searchred'>Your search found 10 matches for </SPAN><SPAN CLASS='TitleBlue'>blondie</SPAN><SPAN CLASS='Searchred'> </SPAN><SPAN CLASS='TitleBlue'></SPAN><SPAN CLASS='Searchred'>. <br />There are 9 cassettes <i>and</i> 1 CD available.</SPAN></CENTER></P>



</BODY></HTML>

otrsteve
08-13-2002, 09:51 PM
Adios,

Here's the test page.

----------------------

############################ TEST PAGE HTML ############################################
<!-- SAVE THIS PAGE AS TestWindow.html -->

<HTML><HEAD><TITLE>OrderFormTestWindow</TITLE>



<script language=javascript type="text/javascript">


// Initialize variables

var test;
var Display;
var userPlat;
var navInfo;


// Determine operating system to be used to adjust textarea column size


function getOS()
{
var userPlat = "unknown";
var navInfo = navigator.userAgent;


if ((navInfo.indexOf ("Win") != -1)
|| (navInfo.indexOf ("windows") != -1))
{
userPlat = "windows";
}
else if (navInfo.indexOf ("Mac") != -1)
{
userPlat = "Mac";
}
return userPlat;
}


// Determine browser name and version to adjust textarea column size

function setitemsArea()
{
var userOS = getOS();
if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5 && userOS !="Mac")
{
itemsArea = 45;
return itemsArea;
}
if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5)
{
itemsArea = 38;
return itemsArea;
}
if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5)
{
itemsArea = 60;
return itemsArea;
}
if (navigator.appName == "Microsoft Internet Explorer" && userOS != "Mac")
{
itemsArea = 50;
return itemsArea;
}
if (navigator.appName == "Microsoft Internet Explorer" && userOS == "Mac")
{
itemsArea = 80;
return itemsArea;
}
}



</script>

</head>

<!-- HTML for displaying the page with the textarea to be written to by the
opener window -->

<body bgcolor="#ffffff" leftmargin="0" marginwidth="0" topmargin="10" marginheight="5">


<table border="0" cellpadding="3" cellspacing="3" width="468">
<tr>
<td width="452" align="center">

<form name="OrderForm">

<script>
var itemsArea = setitemsArea();
document.write("<textarea name='itemsOrdered' cols='");
document.write(itemsArea);
document.write("'rows='10' tabindex='17' wrap='virtual'>Place catalog items below. Indicate total number of items on the first line. Skip a line, then list your order.</textarea>");</script> </form>

</td>
</tr>
</table>
</body>

</html>

otrsteve
08-18-2002, 09:00 PM
To those of you might have been following my original question. I did find a solution that seems to work. I created a frameset with 2 frames. One of the frames is only 1 pixel wide with no content and the background is the same as the main frame that will actually be used. Give it a name such as SaveReference.

When the new window is opened from the main frame, the newwindow= window.open() reference is written to the 1 pixel wide frame, such as:
parent.SaveReference.OrderWindow = OrderWindow. I use a variable, OrderWindow to hold the reference to the new window opened in the main frame.

This allows the main frame to be changed as often as you want. Each time this frame is rewritten, it references the SaveReference frame to obtain the reference to the new window it has previously created:
OrderWindow = parent.SaveReference.OrderWindow;

The new window ( I call it OrderWindow) uses an onunload event to inform the SaveReference frame when it is closed. I use a variable, winOpen, in the SaveReference frame to be either 1 (open) or 0 (closed).

onUnload="window.opener.parent.SaveReference.winOpen='0';

If you think this might be of some help to you, and would like to see the rest of the code, just post a reply.