Solved

Set Global Array/Variable from inside a Javascript Function

Posted on 2011-02-17
9
438 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now