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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using variable in if statement, testing if window is open

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi,

I have hit a wall with how to use variable names inside certain statements (such a "if"). There are a few others I have issues with but to keep it simple, this shows the problem.

Here are two nearly identical pages. Each attempts to open Google in a new window. A function - openwindow(windowurl, windowname) - takes arguments to be used for the URL of Google, and a name to give the new window. The function is called like this: openwindow('
In one variation a variable 'handle' has its value set to 'google_window' google_window is also declared outside of the function with var google_window;

Inside the function a test is used to see if the window is already open. This is inside an if statement. This is where it gets problematic. If I use...

if (handle == undefined || handle.closed) {
handle=window.open(windowurl,windowname);
}

... it does not work, however if hardcoded like this, it works...

if (google_window == undefined || google_window.closed) {
handle=window.open(windowurl,windowname);
}

Note that using handle as the window identifier in the statement inside the if works. So handle does correctly evaluate to mean google_window.

All this might be clearer to see the actual code. Here are the two variations:

<script type="text/javascript">
var google_window;
function openwindow(windowurl, windowname) {
var handle=windowname + '_window';
alert(handle);
if (handle == undefined || handle.closed) {
handle=window.open(windowurl,windowname);
}
}
openwindow('</script>


... and ...

<script type="text/javascript">
var google_window;
function openwindow(windowurl, windowname) {
var handle=windowname + '_window';
alert(handle);
if (google_window == undefined || google_window.closed) {
handle=window.open(windowurl,windowname);
}
}
openwindow('</script>

To see the real deal in action, visit these pages, you can view source on these to see all the code:

(works)

(does not work)

You might get the pop up blocker get in the way on the one that works.


No error is apparent, just that it does nothing. Either one displays the alert that says google_window - so we know JavaScript is working. The problem boils down to why using a variable inside the if( does not work.

And by-the-way I am doing all this because the larger part of the project would use the function to open a number of windows of which the URLs are determined on the fly.

Thanks,
KB
 
I don't know why that should be. This works:
Code:
var handle=42;
if (handle>12) {
 alert( "42>12");
}

_________________
Bob Rashkin
 
Hi

Code:
[b]function[/b] [COLOR=darkgoldenrod]openwindow[/color][teal]([/teal]windowurl[teal],[/teal] windowname[teal])[/teal] [teal]{[/teal]
  [highlight][b]var[/b] handle[teal]=[/teal]windowname [teal]+[/teal] [green][i]'_window'[/i][/green][teal];[/teal][/highlight]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]handle[teal]);[/teal]
   [highlight pink][b]if[/b][/highlight] [teal]([/teal][highlight pink]handle [teal]==[/teal] undefined[/highlight] [teal]||[/teal] handle[teal].[/teal]closed[teal])[/teal]  [teal]{[/teal]
     handle[teal]=[/teal]window[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal]windowurl[teal],[/teal]windowname[teal]);[/teal]
   [teal]}[/teal]
[teal]}[/teal]
[ul]
[li]How could [highlight pink]the comparison[/highlight] ever evaluate to [tt]false[/tt] if you defined handle just [highlight]two lines earlier[/highlight] ?[/li]
[li]How could handle remember values set in previous calls of the openwindow() function, while it is a local variable ?[/li]
[/ul]
So it will be always defined, will contain a string, so will never have closed property.

Personally I would just remove all the window opening and let the visitor decide how he opens the link. But if you want to continue this way, I suggest a global array for window objects :
Code:
[b]var[/b] windowlist[teal]=[/teal][teal]{}[/teal][teal];[/teal]
[b]function[/b] [COLOR=darkgoldenrod]openwindow[/color][teal]([/teal]windowurl[teal],[/teal]windowname[teal])[/teal]
[teal]{[/teal]
  [b]if[/b] [teal](!([/teal]windowname [b]in[/b] windowlist[teal])[/teal] [teal]||[/teal] windowlist[teal][[/teal]windowname[teal]].[/teal]closed[teal])[/teal]  [teal]{[/teal]
    windowlist[teal][[/teal]windowname[teal]]=[/teal]window[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal]windowurl[teal],[/teal]windowname[teal]);[/teal]
  [teal]}[/teal]
[teal]}[/teal]
[COLOR=darkgoldenrod]openwindow[/color][teal]([/teal][green][i]'[URL unfurl="true"]http://google.com/'[/URL][/i][/green][teal],[/teal][green][i]'google'[/i][/green][teal]);[/teal]


Feherke.
 
Good points.

What I presented here is just a piece of much larger code. The variables presenting windows are placed global in the live version. But the same problem exists. This example I've provided here represents the problem.

google_window in this example is declared outside the function. It is "assembled" again in the function since in real usage it is not know ahead of time which window to use.

 
Hi

Trusts said:
google_window in this example is declared outside the function.
Yes. Then never used.
Code:
[highlight][b]var[/b] google_window[teal];[/teal][/highlight]
[b]function[/b] [COLOR=darkgoldenrod]openwindow[/color][teal]([/teal]windowurl[teal],[/teal] windowname[teal])[/teal] [teal]{[/teal]
  [highlight pink][b]var[/b] handle[teal]=[/teal]windowname [teal]+[/teal] [green][i]'_window'[/i][/green][teal];[/teal][/highlight]
   [b]if[/b] [teal]([/teal]google_window [teal]==[/teal] undefined [teal]||[/teal] google_window[teal].[/teal]closed[teal])[/teal]  [teal]{[/teal]
     handle[teal]=[/teal]window[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal]windowurl[teal],[/teal]windowname[teal]);[/teal]
   [teal]}[/teal]
[teal]}[/teal]
[COLOR=darkgoldenrod]openwindow[/color][teal]([/teal][green][i]'[URL unfurl="true"]http://www.google.com'[/URL][/i][/green][teal],[/teal][green][i]'google'[/i][/green][teal]);[/teal]
You have two variables :
[ul]
[li][highlight]Global variable with name google_window and value [tt]undefined[/tt].[/highlight][/li]
[li][highlight pink]Local variable with name handle and value 'google_window'.[/highlight][/li]
[/ul]
There is absolutely no relation between those two.

Later you test the variable google_window then assign a new value to variable handle. So variable google_window still gets nothing.


Feherke.
 
Hi

Anyway, if you prefer to continue with your code, here is a hint : global variables can be accessed as properties of the window object :
Code:
[gray]// after this :[/gray]

[b]var[/b] somevar[teal]=[/teal][green][i]'somevalue'[/i][/green]

[gray]// all these are referring to the same value :[/gray]

[COLOR=darkgoldenrod]alert[/color][teal]([/teal]somevar[teal])[/teal]

[gray]// or[/gray]

[COLOR=darkgoldenrod]alert[/color][teal]([/teal]window[teal].[/teal]somevar[teal])[/teal]

[gray]// or[/gray]

[COLOR=darkgoldenrod]alert[/color][teal]([/teal]window[teal][[/teal][green][i]'somevar'[/i][/green][teal]])[/teal]

[gray]// or[/gray]

[b]var[/b] somename[teal]=[/teal][green][i]'somevar'[/i][/green]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]window[teal][[/teal]somename[teal]])[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top