Global variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chirs

    Global variable

    Hi,

    If I have a global variable in one <SCRIPT>...</script>, is it
    available in the 2nd <SCRIPT>...</script>. I do not know if "global"
    means the whole document or the whole <SCRIPT>...</script> only.

    Chris
  • Lee

    #2
    Re: Global variable

    chirs said:[color=blue]
    >
    >Hi,
    >
    >If I have a global variable in one <SCRIPT>...</script>, is it
    >available in the 2nd <SCRIPT>...</script>. I do not know if "global"
    >means the whole document or the whole <SCRIPT>...</script> only.[/color]


    <html>
    <body>

    <script type="text/javascript">
    alpha="Hello, world!";
    </script>

    <script type="text/javascript">
    alert(alpha);
    </script>

    It's often easier to test than to ask.

    </body>
    </html>

    Comment

    • Douglas Crockford

      #3
      Re: Global variable

      > If I have a global variable in one <SCRIPT>...</script>, is it[color=blue]
      > available in the 2nd <SCRIPT>...</script>. I do not know if "global"
      > means the whole document or the whole <SCRIPT>...</script> only.[/color]

      First of all, chirs (if that's your real name), you should get in the habit of
      getting your capitalization right.

      <SCRIPT>...</script>

      is at best sloppy, and sometimes terrible wrong. You usually want to use all
      lower case.

      In browsers, the so-called window object is the global object. All of the
      scripts of a document will share the same window object.



      Comment

      • Rosalie Mignon

        #4
        Re: Global variable

        "global" variables are really members of the default object (window).
        So, outside of a function, the following 3 statements are equivalent:
        x=1
        var x=1
        window.x=1

        and inside of a function, the following 2 statements are equivalent:
        x=1
        window.x=1

        You can initialize and access those "global" variables from separate script
        blocks, wherever window is available in fact.

        Hope this helps
        "chirs" <yma@kicon.co m> a écrit dans le message de
        news:4c22a744.0 312010912.2a422 bf5@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > If I have a global variable in one <SCRIPT>...</script>, is it
        > available in the 2nd <SCRIPT>...</script>. I do not know if "global"
        > means the whole document or the whole <SCRIPT>...</script> only.
        >
        > Chris[/color]


        Comment

        Working...