Results 1 to 4 of 4

Thread: Accessing global variables across multiple js files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Accessing global variables across multiple js files

    I have one js file, i declare a global variable called bigvar

    var bigvar = ""

    Now i use jquery to import another js file

    &.getscript("myscript.js");

    Now in the myscript there is a function called setbigvar()

    function setbigvar(value)
    {
    bigvar=value;
    }

    Now I call setbigvar() from my first js file.

    setbigvar("New value!");

    Then the value of bigvar is drawn on screen, but it still = ""

    why is this.

    How can my imported script myscript.js use variables in the first js file and vice versa maybe??

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Accessing global variables across multiple js files

    Without testing, I'd assume your problem is $.getScript(). If you use script tags (in an HTML document) for inclusion, then all of your js files are present before execution and a situation like what you've described will work as expected. $.getScript() however won't fetch its file until it's called (and even then it's an AJAX call, so you should wait for a successful status before doing anything that's dependent on its contents). So setbigvar() is not present at the time you're calling it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Accessing global variables across multiple js files

    ok but what does this mean then.

    I seen another guy use a getscript to get a js file. at the start of this js file he had a declaration:

    Code:
    var Playtomic={};
    
    (
    function()
    {
    
    //blah blah blah
    })()
    then followed by his functions.

    in the main js file he called this:

    Code:
    function add(){
    Playtomic["Log"]["View"](ptSWFID, ptGUID, ptAPIKEY, document.location);
    }
    Now Playtomic is not a function but a var i assume. But what do the square brackets means, if its not a function then whats the parameters in the brackets being passed to. It seems like he is passing variables from the main js file to the imported one, but i dont know how he done it.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Accessing global variables across multiple js files

    Playtomic is an object. The square brackets are referring to members of the object, and you can assume that Playtomic["Log"]["View"] is a function. Here's a simplified example:
    Code:
    var Playtomic = {};  //creates an object
    Playtomic.Log = {};  //creates an object as a member of Playtomic object
    Playtomic.Log.View = function(b){alert(b);};  //creates a function as a member of Playtomic.Log object
    Playtomic["Log"]["View"]('text');  //calls the function created on the previous line
    If you execute that, you'll get an alert window that says "text". So in the code you've posted, the values of the variables in the parentheses are being passed to whatever function exists at Playtomic["Log"]["View"].

    As for why it works with $.getScript()... When you define a function, its contents are not evaluated (other than for valid syntax); so if you refer to a global variable that doesn't exist yet, that's okay. It's when you call the function that this matters.

Posting Permissions

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



Click Here to Expand Forum to Full Width