Solved

Set Global Array/Variable from inside a Javascript Function

Posted on 2011-02-17
9
444 Views
Last Modified: 2012-05-11
Hi Experts,

Is there some big secret to setting array or variable values within a function for global access?

I'm using jQuery's getJSON()  to return rows from our database. I then loop through and format the values there-in for display. my problem is I need to access this information later in the script.

I've omitted the var keyword before declaring the value and tested window.string to no avail

How can I achieve global scope for an array or indeed variable from within a function?

Thank you
0
Comment
Question by:wilson1000
9 Comments
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34915048
Have you tried with the closures in javascript, I think the answer lies there, in a closure the variable willl be available even after the function exit.

Hope this helps
0
 
LVL 3

Expert Comment

by:imaki06
ID: 34915054
You should declare that variable outside of the function.

Like:

<script type="javascript">
var myarr=new Array();

function myfunc(){
  myarr[0]="first";
}
</script>
0
 
LVL 4

Author Comment

by:wilson1000
ID: 34915056
Thank you shinuq, would you clarify what a closure is please?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 166 total points
ID: 34915127

//This should be available globally

   window.myArray = ["Hello","World"]
0
 
LVL 19

Accepted Solution

by:
Bardobrave earned 167 total points
ID: 34915275
If you declare a variable in the global scope of a page, it will be available to all the page.

Scopes in javascript code are hyerarchical, so when something is defined in a scope, it's available in all children scopes, but not in ancestor's ones.

Your biggest problem here would be if you have your variable defined in a js file and you want to access it from another one, as to my experience different files means completely different scopes although they are loaded in the same page.

To solve this type of problems, you can use hidden inputs in page code to store values and be able to retrieve them from any code point that executes on the same page.

Hope it helps.
0
 
LVL 29

Assisted Solution

by:Badotz
Badotz earned 167 total points
ID: 34915297
Very Bad to pollute the window object in such a way.

Current practice is to declare a namespace for your stuff, keeping things separate from your libraries, etc.:

var G = {
    "ra" : [ "a", 1, "b", 2, "c", 3 ],
    "c123" : 123,
    "etc" : "etc."
}

and then retreive them as needed:

function something() {
    var cra = G.ra; // Get common array
    G.ra = [ "x", 24, "y", 25, "z", 26 ]; // Change common array
}
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34916287

Just for clarification, this is a perfectly acceptable solution:

 window.myArray = ["Hello","World"]

...perhaps it is not clearly understood that, at the outermost level, vars are actually members of the window object and do not cause polution.  You can check this for yourself with the following statements: var G = "!"; alert( window.G );
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34916628
We will have to agree to disagree on that point.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34919057
No worries - glad to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question