3

This may be a really quick answer (or a really stupid question), but I'm trying to store an array of integers so that they can be accessed later locally by another function, at a random time.

Ways I have thought of so far to do this (and their flaws):

  • HTML5 data attributes i.e. data-ids="1,2,3" (can't store an array easily in these)
  • HTML5 localStorage (can only store a string, not an array and would have to convert)
  • a hidden input i.e. <input type="hidden"> (again, can't store an array, have to convert into a string)

Ideally I would like to be able to push values onto this locally stored array with syntax like array.push(value) etc.

Is there an easy way to do this that I'm missing or will I be resorting to hacks? The end use of this array will be comparing with another array of integers to see if any values match, and if there is a match, remove the index from the second array (i.e. it's an array filter).

This array shouldn't be stored on the server because it is different for each user on the client-side. If there is no nice way to do this I'll probably just think about implementing the functionality a different way.

wnajar
  • 747
  • 1
  • 7
  • 27
  • 1
    Lastly... please be honest and tell me if this is a really dumb way to do things :) I won't be offended, it will just save me a lot of time. – wnajar Jun 08 '13 at 20:24
  • That's a matter of scope. Where are you declaring the array? – bfavaretto Jun 08 '13 at 20:25
  • 2
    create a javascript array (in public scope) and store the array in that? – Johan Jun 08 '13 at 20:25
  • I'm declaring the array in a function that submits a post via AJAX, and I need to be able to access the array in a separate polling function that recurs every `n` seconds and retrieves data from the server. I.e. we compare the two arrays on every AJAX poll. – wnajar Jun 08 '13 at 20:26
  • use a singleton/global variable? – cmt Jun 08 '13 at 20:27

4 Answers4

6

You could, of course, just use window.somevarname = [1,2,3]

Alternatively, if by "elsewhere" you mean on a completely different pageload, then your best bet would be to run it through JSON.stringify() and drop it in localStorage, then JSON.parse() it out.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • It doesn't have to be saved across pageloads, fortunately, just across AJAX polling intervals. I think `localstorage` might be overkill then. Storing as a property of `window` sounds nice, didn't think of that. – wnajar Jun 08 '13 at 20:28
  • Is there any advantage/disadvantage to storing in the window vs. as a global? I've been reading this: http://stackoverflow.com/questions/4862193/javascript-global-variables – wnajar Jun 08 '13 at 20:35
  • 1
    @wnajar They are identical. A global variable is a property of the window. – Niet the Dark Absol Jun 08 '13 at 20:35
  • Just implemented this and results are exactly as desired. Thank you everyone! – wnajar Jun 08 '13 at 20:41
1

Just declare a global variable in your html head section

<script type="text/javascript">
var MyGlobalVariable = [1,2,3];
// or window.MyGlobalVariable = [1,2,3];
</script>

Now you can access it throughout the page.

Nagarjun
  • 2,346
  • 19
  • 28
  • Aren't globals to be avoided? I'm wary of them from PHP but have never used a global in JS. – wnajar Jun 08 '13 at 20:27
  • 1
    Need not be if your variable is properly named without conflicting others. For example jQuery defines "jQuery" as global variable. – Nagarjun Jun 08 '13 at 20:28
  • 1
    @wnajar - All of the other solutions you explored are also essentially the same thing as global variables, just disguised in different ways. So just use a global variable. There's nothing wrong with it when it is exactly what you need. – Michael Geary Jun 08 '13 at 20:30
  • The "global variables must not be used" sentiment exists because they can easily make code hard to maintain. But, there are many things that are essentially global variables disguised in different ways (as Michael Gaery said above), such as singletons. It's okay to use global variables, as long as you do so properly and with caution--ask yourself the necessary questions: Does this variable need to be global? Could I just pass the variable as a parameter to the functions that need it? For more guidelines see http://c2.com/cgi/wiki?GlobalVariablesAreBad – cmt Jun 08 '13 at 20:37
0

create a javascript variable in the global scope(or whereever the array is needed) and you can access it by it's name.

Johan
  • 8,068
  • 1
  • 33
  • 46
0

It's totally fine to store data in global java script variables, but the deciding factor is how you navigate between pages. Javascript is typically not a good choice to store state if you're planning on doing full page refreshes. However it works well for Ajax UIs where global variables are preserved when sub sections on the larger page are loaded

TGH
  • 38,769
  • 12
  • 102
  • 135
  • It's a fully AJAX UI, so I don't need to store between page refreshes. Only between AJAX polling events every interval of `n` seconds. – wnajar Jun 08 '13 at 20:30
  • Ok, so you should be fine storing it as a global variable then. Using an array will make your solution browser agnostic as well – TGH Jun 08 '13 at 20:31