Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Declaring a global variable inside a funtion

    Hi, is there any way to declare a global variable from inside a function? It needs to be created in the onload = function() {} but then I also need to be able to use it around my page.
    Thanks in advance
    Rob

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Don't think so

    But you could declare the variable before the onload and let the function change its value...


    Any reason(s) why you don't want to declare it first?

  • #3
    New Coder
    Join Date
    Feb 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL!! I was being an idiot. I was declaring it inside and instantiating it as an object of a class that I'd made. Every time I took the declaration/instantiation outside the onload function it all went silly.
    You were, absolutely right. I just needed to take the declaration outside and leave the instantiation on the inside.
    All working lovely now
    ThAnK YoU VeRy mUcH !1!
    Rob

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Or you could assign it as a child of the window object. That also makes it global.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •