Jump to content

Access a Javascript array from another page?


maxiscool

Recommended Posts

So I have a webpage that is used to gather information from the user and in the end a 2 dimensional array is created that stores all of this data. I need to now send this 2 dimensional array to a different webpage so it can use this data. How do I do this with JavaScript. I don't want to introduce any server side languages and the data is not vital so security doesn't really matter.What I have gathered so far is:1. Put the data in a URL and then parse it in the other page. This probably wont work because the data is a 2 dimensional array and can get way to big to fit in a URL. The URL would look ridiculous. 2. Put the data in a cookie? I have never worked with cookies so I don't know how this works. Is it even possible to store a 2 dimensional array in a cookie?3. Have both webpages use the same javascript file? - Would this even work, both would have access to the array.Thank you.

Link to comment
Share on other sites

Look into serializing the array into a JSON string, then storing the JSON in a cookie, and on the other page reading the cookie and turning the JSON string back into the original object.http://json.org/Douglas Crockford's Javascript library will convert objects to and from JSON strings:https://github.com/douglascrockford/JSON-js

Link to comment
Share on other sites

Alright so I'm having trouble setting up the cookie. I get the string set up and I believe I have the syntax correct. Right now my website I'm working on is located on my hard drive. I'm wondering if this affects how cookies work?I have home.html and schedule.html in the same folder. I need to send the cookie from home.html to schedule.html, would the path portion of the cookie simply by path=/schedule.html?Is there anyway I can tell if the cookie was successfully added? Whenever I try alert(document.cookie) it turns on blank.One more question, when I serialized the array it gave every element in the array quotation marks will this work a cookie?

Link to comment
Share on other sites

Okay so I figured out half of the problem, I have been using Google Chrome to develop my website and apparently Google Chrome does not support local cookies. Now I just need to figure out how to get this cookie to the other webpage. Both pages are in the same directory so I tried path=/ and path=/*filename* cant seem to get the other page to recognize the cookie.

Link to comment
Share on other sites

Right now my website I'm working on is located on my hard drive. I'm wondering if this affects how cookies work?
Yes, it does.
I need to send the cookie from home.html to schedule.html, would the path portion of the cookie simply by path=/schedule.html?
The path should be a directory, not a file. If you set the path to "/" then the cookie will be available to all pages on the site. If you give a directory name in the path, then the cookie will be available to all files in that directory.
Is there anyway I can tell if the cookie was successfully added?
Your browser should have a way to show you all cookies that have been set by domain, you can check there for the cookie and look at the data in it. This is part of the reason why you should be testing on a live server somewhere, because it saves cookies by domain. Local files that you just open in a browser don't have a domain.
One more question, when I serialized the array it gave every element in the array quotation marks will this work a cookie?
That's fine.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...