最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass array of data from one webpage to the other? - Stack Overflow

programmeradmin7浏览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. . . . ..

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

Share Improve this question asked Jan 4, 2011 at 4:52 Prateek RajPrateek Raj 3,9946 gold badges41 silver badges51 bronze badges 1
  • Possible duplicate of Pass javascript array to another page – Helen Commented Jan 10, 2017 at 8:33
Add a ment  | 

5 Answers 5

Reset to default 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./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/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, etc) in which you have available some kind of storage across requests (i.e. Session in asp).

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

  • http://code.google./p/sessionstorage/
  • https://gist.github./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./douglascrockford/JSON-js

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

You may use cookie for that purpose.

发布评论

评论列表(0)

  1. 暂无评论