0

I need to transfer a multi-dimensional JavaScript array to another page, without using any library. What I can use is JavaScript, PHP and other languages that doesn't need a library.

I have a three-dimensional array which is build like this: storage[category][field][multiple answers] and has a lot of values.

I need to transfer it to another page so I can get all the values like:

alert(storage[5][4][8]);

=======================================================================

Well, I can pass a normal variable to another page but I cant get the values from my array when I'm testing: storage[1][1][1] for example.

The big question is how I can pass a multidimensional array to another page and still be able to get the values like this: storage[1][1][1]

As I get it I'm forced to pass all the 121 arrays you can se below to be able to access all dimensions in the array.

My array is built up like this:

storage = new Array();
for (var i1=1;i1<12;i1++){
storage[i1] = new Array();
for (var i2=1;i2<12;i2++){
storage[i1][i2] = new Array();
}
}
2
  • transfer to another page Could you give us a use case? Commented Jan 11, 2014 at 17:04
  • If you are using 2 JS files and both are included in both the pages , and if your array is globally declared in one of the files , your alert will work Commented Jan 11, 2014 at 17:07

3 Answers 3

1

Without using a library like jQuery, you can convert your array to JSON, pass it via a URL and decode it on the target page. Converting it to JSON would look like:

var json_string = JSON.stringify(your_array);

Then pass it in a URL:

var your_url = "http://www.your_website.com/page.html?json_string=" + json_string;

And you could decode it back to an array like so:

var your_new_array = JSON.parse(getUrlVars()["json_string"]);

For some more reading, check out this JSON page: http://www.json.org/js.html

Sign up to request clarification or add additional context in comments.

Comments

0

JSON.stringify() is supported by all major browsers. Send it to the server via a POST, then have your php retrieve the variable from $_POST and send it back.

Comments

-1

As far as I can see there are two main ways to do what you want:

  1. Pass the array to the webserver, and have it send it back on next request.
  2. Store the data locally in the browser.

The first way could get pretty complicated. You would have to store the data in a database, file or cookie/session.

The second way would be the easiest. Use javascript to store the array in the browser. You can either use a cookie, or use the localStorage object.

Using a cookie would require you to serialize the data manually. It would also get passed to the server, so if you want to save bandwidth, you would want to avoid this.

The localStorage method would only store the data locally, and you also don't need to serialize anything, the browser takes care of this for you.

See the links below for more examples.

http://www.w3schools.com/html/html5_webstorage.asp

http://www.w3schools.com/js/js_cookies.asp

3 Comments

-1 you said w3schools, have a look on w3fools and then start using MDN
@metadings I am perfectly aware w3schools is in most cases absolute crap, but in this case it explained them both pretty clearly.
No. Just NO. Both explanations on w3schools are small minded, the article about cookies even contains syntax errors. w3schools is misleading just like 10 years ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.