Welcome to WebmasterWorld Guest from 54.198.55.167

Forum Moderators: open

Message Too Old, No Replies

javascript variable-scope

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

New User

10+ Year Member

joined:Dec 29, 2004
posts:28
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


You'll have to show us the relevant functions.
12:45 pm on Apr 7, 2005 (gmt 0)

New User

10+ Year Member

joined:Dec 29, 2004
posts:28
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


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

debugwin.length is always incremented.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members