开发者

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

T开发者_JS百科hank you Guys. . . . ..


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


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


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

  • http://code.google.com/p/sessionstorage/
  • https://gist.github.com/350433

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:

  • https://github.com/douglascrockford/JSON-js


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


You may use cookie for that purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜