javascript scope query

Discussion in 'NZ Computing' 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. Ralph Fox Guest

    On Tue, 05 Jul 2011 13:24:32 +1200, in message <>
    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
     
    Ralph Fox, Jul 5, 2011
    #2
    1. Advertising

  3. Guest

    On Tue, 05 Jul 2011 20:22:53 +1200, Ralph Fox <>
    wrote:

    >On Tue, 05 Jul 2011 13:24:32 +1200, in message <>
    > 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
     
    , Jul 5, 2011
    #3
  4. Ralph Fox Guest

    On Tue, 05 Jul 2011 22:48:19 +1200, in message <>
    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
     
    Ralph Fox, Jul 6, 2011
    #4
  5. Guest

    On Wed, 06 Jul 2011 19:59:15 +1200, Ralph Fox <>
    wrote:

    >On Tue, 05 Jul 2011 22:48:19 +1200, in message <>
    > 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
     
    , Jul 6, 2011
    #5
    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. Global Scope

    , Jun 11, 2005, in forum: MCSE
    Replies:
    4
    Views:
    909
  2. kcsteele

    changing group scope

    kcsteele, Apr 7, 2006, in forum: MCSE
    Replies:
    2
    Views:
    4,300
    kcsteele
    Apr 11, 2006
  3. Replies:
    4
    Views:
    55,467
    gsingle
    Jul 14, 2006
  4. Lars Bonnesen
    Replies:
    8
    Views:
    1,929
    Lars Bonnesen
    Jun 15, 2006
  5. Shane

    To Javascript, or not to Javascript

    Shane, Aug 29, 2005, in forum: NZ Computing
    Replies:
    5
    Views:
    567
    Waylon Kenning
    Aug 30, 2005
Loading...

Share This Page