0

i'm trying to send three arrays of data from one .js file which is used by first webpage to the other .js file which is used by the second webpage.

the data in the first webpage is dynamically built so those three arrays are to be sent to the next webpage.

can anyone suggest some javascript code or tutorial.

please help...............

Thank you Guys. . . . ..

Prateek Raj
  • 3,966
  • 6
  • 39
  • 50
  • Possible duplicate of [Pass javascript array to another page](http://stackoverflow.com/questions/29498029/pass-javascript-array-to-another-page) – Helen Jan 10 '17 at 08:33

5 Answers5

5

I'd suggest using the JSON data format. JSON is like XML except a lot easier to parse through. Some great examples can be found on Jquery's page:

http://api.jquery.com/jQuery.getJSON/

Everything you need to read the JSON feed can be found on jQuery. If you need to know how to structure a JSON feed you can read about it here:

http://www.json.org/js.html

Transition
  • 130
  • 7
2

This is really tough to do with strictly javascript and html. Here are some options:

  1. You could store the array in a hidden form variable and post it to the destination page
  2. If the dataset is small enough (< 4K), then you can store it in a cookie across requests.
  3. If you are only using the most modern browsers (read: HTML5), you can use localstorage
  4. You could encode the data and pass it in the url

In general, though, these are mostly hacks. Usually this kind of work is augmented by some type of server-side processing (perl, php, asp.net, etc) in which you have available some kind of storage across requests (i.e. Session in asp.net).

Bobby D
  • 2,129
  • 14
  • 21
1

You could use the Web Storage API, and include a polyfill to port the functionality for older browsers:

Both of these use window.name to provide a session-like state. This may or may not be secure enough for your needs.

From there, you can use the following code for all browsers:

// Store on previous page
sessionStorage.setItem("yourArray", JSON.stringify(yourArray));

// Restore on following page
var yourArray = JSON.parse(sessionStorage.getItem("yourArray"));

EDIT: Older browsers may need the following for the above code sample. This is so the array can be serialized to a string, since sessionStorage only supports string key-value pairs:

Stoive
  • 11,232
  • 4
  • 24
  • 32
0

Check out jQuery.data() and friends. Cf.: http://api.jquery.com/category/data/

Moishe Mo
  • 86
  • 5
0

You may use cookie for that purpose.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124