Where can I declare Global Variables

Discussion in 'Javascript' started by rick, Apr 18, 2004.

  1. rick

    rick Guest

    Noob problem. I prefer to keep all my scripts in an external '.js' file.
    I am currently loading the external '.js' file from the header. Problem is I
    would like to declare
    a global variable in the external file, but I keep getting an error about
    the object does not exist.

    Can someone tell me where or how to declare a global variable in an external
    file that is
    available after the page is loaded.

    Thanks in advance.
    rick, Apr 18, 2004
    #1
    1. Advertising

  2. rick

    rick Guest

    I found the answer: Do not use 'var' to declare the variable.
    Just declare it!! i.e. 'newvar = 0;'

    "mscir" <> wrote in message
    news:...
    > rick wrote:
    >
    > > Noob problem. I prefer to keep all my scripts in an external '.js' file.
    > > I am currently loading the external '.js' file from the header. Problem

    is I
    > > would like to declare
    > > a global variable in the external file, but I keep getting an error

    about
    > > the object does not exist.
    > >
    > > Can someone tell me where or how to declare a global variable in an

    external
    > > file that is
    > > available after the page is loaded.
    > >
    > > Thanks in advance.

    >
    > - Variables declared within a function are local to that function
    > (accessible only within that function)
    >
    > http://lists.evolt.org/archive/Week-of-Mon-20030303/136552.html
    >
    > ... variables declared withing a function are defined only within the
    > body of the function. They are local variables and have local scope.
    > Function parameters also count as local variables and are defined only
    > within the body of the function.
    >
    > - Variables declared outside a function are global
    > (accessible from anywhere on the page)
    >
    > If you keep getting an error post your code or a url where it can be seen.
    > Mike
    >
    rick, Apr 18, 2004
    #2
    1. Advertising

  3. "rick" <rmsingh@no_spam_rogers.com> writes:

    Please don't top post.

    > I found the answer: Do not use 'var' to declare the variable.
    > Just declare it!! i.e. 'newvar = 0;'


    That shouldn't make a difference, if the declaration is not inside a
    function body. Then the declaration will declare the global variable,
    just as simply assigning to an undeclared variable.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 18, 2004
    #3
  4. rick

    rick Guest


    > > I found the answer: Do not use 'var' to declare the variable.
    > > Just declare it!! i.e. 'newvar = 0;'

    >
    > That shouldn't make a difference, if the declaration is not inside a
    > function body. Then the declaration will declare the global variable,
    > just as simply assigning to an undeclared variable.


    That is what I did. Sorry still learning..........
    rick, Apr 19, 2004
    #4
  5. On Sun, 18 Apr 2004 17:35:53 -0700, mscir
    <> wrote:

    > rick wrote:
    >>>> I found the answer: Do not use 'var' to declare the variable.
    >>>> Just declare it!! i.e. 'newvar = 0;'
    >>>
    >>> That shouldn't make a difference, if the declaration is not inside a
    >>> function body. Then the declaration will declare the global variable,
    >>> just as simply assigning to an undeclared variable.

    >>
    >> That is what I did. Sorry still learning..........

    >
    > I think rick is right, declaring a variable without using "var" inside a
    > function makes the variable global, at least it looks that way to me.


    He is, and Lasse agreed: "...assigning to an undeclared variable". That
    is, a variable without prior use of var. It's also described in Netscape's
    JavaScript Guide:

    <URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/ident.html#1008330>

    [snip]

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
    Michael Winter, Apr 19, 2004
    #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. khawar
    Replies:
    1
    Views:
    7,265
    GastonQ
    Jul 10, 2003
  2. Wayne
    Replies:
    2
    Views:
    453
    Wayne
    Nov 11, 2003
  3. Brian Sammon

    declare vs define of global variables

    Brian Sammon, Oct 5, 2004, in forum: C Programming
    Replies:
    7
    Views:
    477
    Flash Gordon
    Oct 8, 2004
  4. jubelbrus
    Replies:
    5
    Views:
    596
    JohnQ
    Jul 20, 2007
  5. G Iveco
    Replies:
    6
    Views:
    479
    Mike Treseler
    Aug 7, 2007
Loading...

Share This Page