variable scope

R

Reg

var scope = "global scope";
function checkscope(){
var scope = "local scope";
document.write(scope);
}
checkscope();
document.write(scope);

as you would expect results in
local scopeglobal scope

while

var scope = "global scope";
function checkscope(){
scope = "local scope";
document.write(scope);
}
checkscope();
document.write(scope);

as you would expect results in
local scopelocal scope

Leaving off the var within the function has changed the global
variable rather than setting up a new local variable

So why does the following code not work the same way?

var strReturn1 = "check scope";
function displayNames(){
...
...
strReturn1= "<select>";
...
...
strReturn1=strReturn1 + "</select>";
document.getElementById('col-Names').innerHTML=strReturn1;
}
document.getElementById('col-Names').innerHTML=strReturn1;


document.getElementById('col-Entries').innerHTML=strReturn1;
within the function displays the selection list but the same line
outside the function displays the string "check scope"


But surely strReturn1 the way it is coded here has global scope and
should have been changed by the function????

Using alert or document.write instead of getElementById produce the
same result

I guess I must have a blind spot somewhere.
I would be grateful if someon can identify it for me.

Reg
 
E

Elegie

On 05/07/2011 08:32, (e-mail address removed) wrote :

Hi Reg,
document.getElementById('col-Entries').innerHTML=strReturn1;
within the function displays the selection list but the same line
outside the function displays the string "check scope"

But surely strReturn1 the way it is coded here has global scope and
should have been changed by the function????

You are using a FunctionDeclaration to declare your function. This means
that your function is evaluated and made available at any point of your
script, before the first statement is executed. Would you happen to call
this function before you make the global variable assignment? :)

If this is not the case, then could you post some test case
demonstrating the issue? As we cannot reproduce the error, it is
difficult to guess what goes wrong.

Also, if you are in good shape, have slept well and got a whole bunch of
coffees already, try and read the following (something I posted recently):

<URL:http://groups.google.com/group/comp.lang.javascript/msg/66e9455bc5edac34>

Regards,
Elegie.
 
R

Reg

On 05/07/2011 08:32, (e-mail address removed) wrote :

Hi Reg,


You are using a FunctionDeclaration to declare your function. This means
that your function is evaluated and made available at any point of your
script, before the first statement is executed. Would you happen to call
this function before you make the global variable assignment? :)

If this is not the case, then could you post some test case
demonstrating the issue? As we cannot reproduce the error, it is
difficult to guess what goes wrong.

Also, if you are in good shape, have slept well and got a whole bunch of
coffees already, try and read the following (something I posted recently):

<URL:http://groups.google.com/group/comp.lang.javascript/msg/66e9455bc5edac34>

Regards,
Elegie.

Thanks Elegie

I was calling the function from onLoad
Calling it from within the script has fixed the problem.

Regards
Reg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top