Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > NZ Computing > javascript scope query

Reply
Thread Tools

javascript scope query

 
 
Reg@nospam.com
Guest
Posts: n/a
 
      07-05-2011

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
 
Reply With Quote
 
 
 
 
Ralph Fox
Guest
Posts: n/a
 
      07-05-2011
On Tue, 05 Jul 2011 13:24:32 +1200, in message <(E-Mail Removed)>
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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


strReturn1 is changed when the function is called/invoked/executed,
not when the function is defined.

This shows you defining the function displayNames, but
does not show you calling/invoking/executing the function.

Until you actually call/invoke/execute displayNames, the value of
strReturn1 will still be "check scope". The script inside the
function definition is not executed until you call the function
from somewhere else.

--
Kind regards
Ralph
 
Reply With Quote
 
 
 
 
Reg@nospam.com
Guest
Posts: n/a
 
      07-05-2011
On Tue, 05 Jul 2011 20:22:53 +1200, Ralph Fox <(E-Mail Removed)>
wrote:

>On Tue, 05 Jul 2011 13:24:32 +1200, in message <(E-Mail Removed)>
>(E-Mail Removed) wrote:
>
>> 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????

>
>strReturn1 is changed when the function is called/invoked/executed,
>not when the function is defined.
>
>This shows you defining the function displayNames, but
>does not show you calling/invoking/executing the function.
>
>Until you actually call/invoke/execute displayNames, the value of
>strReturn1 will still be "check scope". The script inside the
>function definition is not executed until you call the function
>from somewhere else.



Thanks very much Ralph

Calling the function within the script fixes the problem.
The situation was a little more involved than I showed.
I had been calling it from
<body onLoad="displayNames()">
along with some other functions all of which I wanted to contribute to
the same innerHTML

If I now understand it correctly, the browser's parser established the
inner html as "check scope" before onLoad was called and did not
execute the whole script again ??

Kind regards
Reg
 
Reply With Quote
 
Ralph Fox
Guest
Posts: n/a
 
      07-06-2011
On Tue, 05 Jul 2011 22:48:19 +1200, in message <(E-Mail Removed)>
(E-Mail Removed) wrote:
>
> Thanks very much Ralph
>
> Calling the function within the script fixes the problem.
> The situation was a little more involved than I showed.
> I had been calling it from
> <body onLoad="displayNames()">
> along with some other functions all of which I wanted to contribute to
> the same innerHTML
>
> If I now understand it correctly, the browser's parser established the
> inner html as "check scope" before onLoad was called and did not
> execute the whole script again ??


Yes.

The browser's parser executed the script except for the bodies of
function definitions.
The onLoad executed the body of the function which it called, but not
the rest of the script.

--
Kind regards
Ralph
 
Reply With Quote
 
Reg@nospam.com
Guest
Posts: n/a
 
      07-06-2011
On Wed, 06 Jul 2011 19:59:15 +1200, Ralph Fox <(E-Mail Removed)>
wrote:

>On Tue, 05 Jul 2011 22:48:19 +1200, in message <(E-Mail Removed)>
>(E-Mail Removed) wrote:
>>
>> Thanks very much Ralph
>>
>> Calling the function within the script fixes the problem.
>> The situation was a little more involved than I showed.
>> I had been calling it from
>> <body onLoad="displayNames()">
>> along with some other functions all of which I wanted to contribute to
>> the same innerHTML
>>
>> If I now understand it correctly, the browser's parser established the
>> inner html as "check scope" before onLoad was called and did not
>> execute the whole script again ??

>
>Yes.
>
>The browser's parser executed the script except for the bodies of
>function definitions.
>The onLoad executed the body of the function which it called, but not
>the rest of the script.


Thanks again

Reg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments