Solved

Set Global Array/Variable from inside a Javascript Function

Posted on 2011-02-17
9
440 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:shinuq
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
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 166 total points
ID: 34915127

//This should be available globally

   window.myArray = ["Hello","World"]
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now