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