variable scope

Discussion in 'Javascript' started by Reg@nospam.com, Jul 5, 2011.

  1. Guest

    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
    , Jul 5, 2011
    #1
    1. Advertising

  2. Elegie Guest

    On 05/07/2011 08:32, 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.
    Elegie, Jul 5, 2011
    #2
    1. Advertising

  3. Guest

    On Tue, 05 Jul 2011 10:52:24 +0200, Elegie <>
    wrote:

    >On 05/07/2011 08:32, 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.


    Thanks Elegie

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

    Regards
    Reg
    , Jul 5, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    934
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    654
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    463
  4. David Filmer
    Replies:
    19
    Views:
    227
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    194
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page