Forum Moderators: open

Message Too Old, No Replies

javascript variable-scope

         

tores

11:39 am on Apr 7, 2005 (gmt 0)

10+ Year Member



Hi.

When loading a page I include this js-file so I can use it's functions later on. In this file i have a global variable, whisch is an array.
Later on I call a debugging function in this file 2 times, each time creating a new popup-window with some debugging-info. The references to the windows is stored in the global array (using array.unshift()).
However later on when accessing the array only the last window-object is contained in the array, and the array.length is 1, not 2 as expected. It seems that when calling my debug-function a second time the reference to the first window is "forgotten". Why is this? Shouldn't the global array be able to hold a reference across function-calls?

regards tores

Bernard Marx

12:24 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to show us the relevant functions.

tores

12:45 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



This is part of the file I'm talking about. debugwin is global and opendebugwindow is the function called twice. As explained earlier the second time I'm calling it the alert statement should print 2, not 1.

regards tores


var debugwin = new Array();

//*********************//
// Debugging functions //
//*********************//

function opendebugwindow(debugmsg){

if(navigator.appName == "Netscape"){
var name = debugmsg;
}else if(navigator.appName == "Microsoft Internet Explorer"){
var name = alphabetize(debugmsg);
}else{
alert("Could not show debuginfo because the browser, "+navigator.appName+", is unknown");
return;
}

debugwin.unshift(window.open('', name, 'height=600, width=400, scrollbars=yes, resizable=yes'));
/* Always prints 1! */
alert(debugwin.length);
debugwin[0].document.write(debugmsg);
debugwin[0].document.close();

if(navigator.appName == "Netscape")
debugwin[0].addEventListener("keydown", ff_eventhandler, true);
else
debugwin[0].document.onkeypress = ie_eventhandler;
}

Bernard Marx

1:01 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not getting that behaviour (IE6 & FF).

debugwin.length is always incremented.