Solved

Set Global Array/Variable from inside a Javascript Function

Posted on 2011-02-17
9
443 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…

831 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