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 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