Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does an Object Variable exist?

Status
Not open for further replies.

pcawdron

Programmer
Joined
Jun 14, 2000
Messages
109
Location
AU
Anyone know how to determine if an object variable is set/exists?

I'm using an object variable that contains another open explorer window, but I need to know if the user has closed that window

Win2=window.open("Sample.htm","","height=100,width=100,top=100,left=100")
Win2.creator = self


Then, later in the program, if the user clicks a button I show the object win2 using...

win2.focus()[


But, if the user has accidentally closed this window I get an object not set error, how can I check to see if the object is initialised before I reference it. If it's been accidentally closed, I could then reopen it before setting it as the focus.

Thanks in advance,
Peter
 
In Javascript, all variables are objects. All functions are objects. All objects (String(), Array(), etc.) are objects.

For your code, try:

//this should be a global variable, outside all functions, or you could lose your ability to reference it

var win2;

if (!win2.closed)
{
if (win2!=null)
{
win2.focus();
}
}

Also, I noticed in the first 2 examples, you capitalized Win2, but in the last, you didn't. Javascript is case sensitive, and you HAVE to keep the upper and lower case letters consistent, or you'll get an Object not found, or Object expected error.
 
Beautiful... works like a treat...

Could you, however, explain the logic behind your steps so I can understand it better. From what I can see it appears that the first test looks to see if the window is closed, the second looks to see it the object is null. Wouldn't it fail the first test if the object was null, making the second test redundant? (I thought they would have been applied the other way around, check to see if it's a valid object and then check to see if, as an object, it's closed...)


if (win2!=null)
{
if (!win2.closed)
{
win2.focus();
}
}


But I'm new to Javascript, comming from a VB background, so I'm still getting my feet wet on this stuff.

Cheers,
Peter

 
Actually, you're right, Peter. The code should be:

if (win2 != null)
{
if (!win2.closed)
{
}
}

I've found that Netscape and IE handle the object returned from a window.open() differently. When the window is closed, Netscape will set the variable to null, and IE will set the object value to .closed.

I was at home, and the code I'd written was at work, and I was too lazy to test which way it should have been.

Thanks for the offer of a star, gerrygerry. I just enjoy the exchange of ideas and like helping out where I can.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top