?
Solved

Set Global Array/Variable from inside a Javascript Function

Posted on 2011-02-17
9
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 664 total points
ID: 34915127

//This should be available globally

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

Accepted Solution

by:
Bardobrave earned 668 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 668 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

762 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