Prateek Raj
Prateek Raj

Reputation: 3996

How to pass array of data from one webpage to the other?

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. . . . ..

Upvotes: 0

Views: 4069

Answers (5)

Stoive
Stoive

Reputation: 11322

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:

Upvotes: 1

Transition
Transition

Reputation: 130

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

Upvotes: 5

Bobby D
Bobby D

Reputation: 2159

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).

Upvotes: 2

Ho&#224;ng Long
Ho&#224;ng Long

Reputation: 10848

You may use cookie for that purpose.

Upvotes: 0

Moishe Mo
Moishe Mo

Reputation: 86

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

Upvotes: 0

Related Questions