View Full Version : How do you pass arrays between html pages?

12-18-2011, 01:45 AM
I have an array that has user accounts and need to pass it to another page. How can i do this? My teacher said we can only use html and javascript.

12-18-2011, 03:02 AM
The big question here is "how did you get the user accounts data?". Using only html and javascript I'm guessing it was user entered via a form. Forms pass data, and that's what I would do. Of course not being able to use PHP makes recovering the data on the next page hard.

just pass the data, don't use arrays.
Look at this article for help: http://www.irt.org/articles/js063/

Old Pedant
12-18-2011, 06:19 AM
That code Sunfighter pointed you to works, but it's really ugly an clumsy if you need to pass lots of form fields. It's silly to have to go look through the entire query string each time you need a value from a name=value pair. Do it one time, instead, and create a lookup table.

I prefer to do this:

<script type="text/javascript">
var qs = []; // where we will put the fields with their values

if ( location.search.length > 2 )
var pairs = location.search.substring(1).split("&");
for ( var p = 0; p < pairs.length; ++p )
var pair = pairs[p].split("=");
qs[ pair[0] ] = decodeURIComponent( pair[1] );

Now that qs[] variable will hold a key/value "dictionary" of all the name=value pairs that were passed from the prior page's <form> in the query string.

So, for example, you could say

var name = qs["name"];

and then use the value obtained as needed in the second page.

CAUTION: That code will *NOT* handle any multiple occurrences of the same name. It's easy to adapt it to do so, if you need to do so.

12-18-2011, 05:15 PM
Is there a way to write to a specific node on a xml file? For example lets say i have the following xml file:


I want to let employees to change their password. So lets say if sam wants to change his password. I want only his password to change. I try to do it but it doesnt work.

Nevermind i got it to work

12-18-2011, 10:23 PM
I have an array that has user accounts and need to pass it to another page. How can i do this? My teacher said we can only use html and javascript.

Hi phenom,

I suggest using function toString() to convert an Array to a String (then you can pass this page to page without any worry, right? It becomes a String), then you can use .split() to get it back as an Array.


//Set an Array contains username and password, then make it a string
var userArray = ["Username", "Password"];
var userString = userArray.toString();

//Send info to process.html
location.href = "process.html?info=" + userString;

Then in file process.html:

//location.search.split["="] will return ["info", userString];
var userString = location.search.split["="][1];

//Now you get the original userArray = ["Username", "Password"];
var userArray = userString.split(",");

I hope this will help.


rnd me
12-19-2011, 04:02 AM
just use localStorage or setCookie and getCookie to share your info.

Old Pedant
12-19-2011, 07:25 AM
For the life of me, I don't understand the point of this.

You can *NOT* share that XML file--or *ANY* data--among multiple users when you are using ONLY client-side (in the browser) coding.

You *MUST* do that with server-side coding. Period. End of statement.

You can add Joe to the XML data on Joe's machine, if you use local storage.

You can add Mary to the XML data on Mary's machine, if you use local storage.

But Joe will never see that Mary has been added and Mary will never see that Joe has been added.

If this is for a school project, okay. Fine. It's 100% silly and a waste of time, but if you must do it, you must. But there is no real world application for it. At all.