declare globals in .JS file

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

    declare globals in .JS file

    If I declared an array or object in a .JS file, should it not be global
    throughout all the files which reference it?


  • Fred Serry

    #2
    Re: declare globals in .JS file


    "Bill" <no@where.com > schreef in bericht
    news:bmmjqb$qda $1@titan.btinte rnet.com...[color=blue]
    > If I declared an array or object in a .JS file, should it not be global
    > throughout all the files which reference it?
    >
    >[/color]

    Hi Bill.

    Short answer: No.

    Long version: A .js file is no more and no less than an include.

    <script type=text/javascript src=a.js><scrip t>
    (a.js being: var a=1)

    --and--

    <script type=text/javascript>var a=1<script>

    are identical.

    If you want a global var or object between pages, use cookies, frames or a
    db serverside.

    Fred



    Comment

    • Lee

      #3
      Re: declare globals in .JS file

      Bill said:[color=blue]
      >
      >If I declared an array or object in a .JS file, should it not be global
      >throughout all the files which reference it?[/color]

      No.
      The .js file is just a text file. It doesn't provide any place to
      store the data.
      As each file loads and interprets the file, each one creates its
      own storage space.

      Comment

      • Chris Riesbeck

        #4
        Re: declare globals in .JS file

        Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<bmmkvf01j or@drn.newsguy. com>...[color=blue]
        > Bill said:[color=green]
        > >
        > >If I declared an array or object in a .JS file, should it not be global
        > >throughout all the files which reference it?[/color]
        >
        > No.
        > The .js file is just a text file. It doesn't provide any place to
        > store the data.
        > As each file loads and interprets the file, each one creates its
        > own storage space.[/color]

        I think it's better to think in terms of windows, not files.
        Variables are attached to window objects. Loading a window
        or frame creates a new window object. The old window
        object is gone, along with its variables. In one window,
        you can load several .js files and they can refer to one
        common set of variables.

        Hence a common technique for "global" variables is to use
        frames and define the variables in the frameset or frame
        that never goes away. Any frame can access the frameset
        variables using top.variablenam e.

        Comment

        • Lee

          #5
          Re: declare globals in .JS file

          Chris Riesbeck said:[color=blue]
          >
          >Lee <REM0VElbspamtr ap@cox.net> wrote in message
          >news:<bmmkvf01 jor@drn.newsguy .com>...[color=green]
          >> Bill said:[color=darkred]
          >> >
          >> >If I declared an array or object in a .JS file, should it not be global
          >> >throughout all the files which reference it?[/color]
          >>
          >> No.
          >> The .js file is just a text file. It doesn't provide any place to
          >> store the data.
          >> As each file loads and interprets the file, each one creates its
          >> own storage space.[/color]
          >
          >I think it's better to think in terms of windows, not files.[/color]

          Yes. That was actually a blunder on my part.
          I had meant to say "As each *window* loads ...".

          Comment

          Working...