0

i am using this for Phonegap so,i would prefer the solution in javascript.

i found many solutions using PHP.thats why i thought of making it clear.

well i get an array,whose vales are from the database.I want to pass this array to the SECOND HTML PAGE.

to pass a variable the following code is being used...

EDIT:

 window.location.href="1stpage.html"?var1=&value;

hope, i am right with the coding.

Please,guide me,if i am wrong.

changes with reference to the answer suggested

Julio Meca Hansen Sir,i tried implementing the same

 window.location.href='1st page.html?var1=5&var2=6'; 

and tried verifying it by printing the values on the second page...

console.log("value1: "+var1 +"value2: "+var2 ); 

alas!,i get this error...

E/Web Console(12476): ReferenceError: Can't find variable: var1 at file:///android_asset/www/1st%20page.html?var1=5&var2=6:17 
user
  • 1,001
  • 4
  • 21
  • 45
  • Your code is invalid - JS is different from HTML, you don't wrap them between `<` and `>` signs ;) – Terry Apr 18 '13 at 12:01
  • oops, Sorry.i may be stand corrected! – user Apr 18 '13 at 12:06
  • This is an unusual scenario since you risk the user being able to alter the data. Usually you'd re-retrieve the data from the database between pages. – NibblyPig Apr 18 '13 at 12:34
  • SLC,i dont think so...because i am using this concept in Phonegap.i am not gonna get any values from the user.it will be from the database. – user Apr 18 '13 at 12:39
  • 1
    @SLC I beg to differ - sometimes passing strings or arrays between pages can be important, such as a multistep form (well, you can technically do that with cookies, but that doesn't make them any more secure). Pagination leverages a lot on passing various variables between pages, too. – Terry Apr 19 '13 at 07:53
  • 1
    There are a bunch of reasons why I would never personally do that, such as server side validation, or the user pressing the 'back' button in the browser, or pressing F5, or if a validation error occured on page 1 and you were on page 4, you'd have to get back there and have a nightmare of ensuring you don't lose the information from page 2, 3, 4 when you press 'next' after correcting the mistake. Lots of unnecessary baggage in my opinion, and a monstrosity to program and maintain. – NibblyPig Apr 19 '13 at 11:11

4 Answers4

2

If this array will only be used by JavaScript, then do NOT use a cookie. Cookies are for storing information that the server needs access to, and setting them willy-nilly can increase your network usage by a significant amount!

Instead, use JavaScript's localStorage interface. It is supported by all modern browsers (IE7 and below don't count as "modern", especially with Windows Update practically forcing you to update), and is real easy to use:

var someArray = [1,2,3,4];
if( !window.localStorage) alert("Sorry, you're using an ancient browser");
else {
    localStorage.myArray = JSON.stringify(someArray);
}

Later, you can retrieve it: JSON.parse(localStorage.myArray)

I hope this is what you are trying to achieve. If not, please clarify your question some ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Kolink Sir,could you please let me know the concept of we using the Window .local storage and why are we gonna use **JSON.stringify(someArray);**....we use JSON when you want some dat from online...AM i right??? – user Apr 18 '13 at 12:37
  • 1
    `localStorage` can only store strings, and does not serialise values automatically. `JSON` is a means to convert JS objects to a string value. Usually it's used for passing data around online, but not necessarily. – Niet the Dark Absol Apr 18 '13 at 13:16
  • Kolink,now how should i connect it to the next HTML page? – user Apr 18 '13 at 13:30
  • 1
    You can just redirect with `location.href = '1stpage.html'`, just like that, and retrieve `localStorage` values from there. – Niet the Dark Absol Apr 18 '13 at 13:44
  • Kolink,i hope you dont mind me asking questions...i am new to javascript.so this code will be on the 1st page. `var someArray = [1,2,3,4]; if( !window.localStorage) alert("Sorry, you're using an ancient browser"); else { localStorage.myArray = JSON.stringify(someArray); }` on the 1st page and later on we should use the following code to retrieve it?? `JSON.parse(localStorage.myArray)` – user Apr 18 '13 at 13:55
1

If the code is linked to an image, it would be more like this:

<img src="images/img1.png" alt="" onclick="window.location.href='1stpage.html?var1=5&var2=6';">

Modify it to fit your needs :P

Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
  • Julio Meca Hansen Sir,thank you.on the first note,i am using mapping,for the on-click....on the second note,i need to pass an **ARRAY** not a variable. – user Apr 18 '13 at 12:04
  • second page should by php, not html if he/she wants to use $_GET to fetch values. – Bojan Kovacevic Apr 18 '13 at 12:04
  • Doesn't matter... I wrote `var1=5` but you could easily write `a[sample1]=5` and so on. The ideal would be for you to automatically build the URL parameters with the `http_build_query()` function, but if you're using plain HTML, you need to specify each parameter by hand, being it an array or a simple value. – Julio María Meca Hansen Apr 18 '13 at 12:06
  • Julio Meca Hansen Sir,i am using the basic html. i will try and let you know whether did it work or not. – user Apr 18 '13 at 12:09
  • Julio Meca Hansen Sir, can i code it this way, `Sun` – user Apr 18 '13 at 12:14
  • 1
    Of course not, you can't pass an array in a direct fashion. `1stpage.html?var nameArray[]=results.rows.item(i).name"` is not a valid parameter. Let's say i equals to 5 and `results.rows.item(i).name` equals to 'dog'. Your parameter would be like `1stpage.html?nameArray[5]=dog`. As you want to pass some elements calculated dynamically, you should build a javascript function that does the same so, instead of writing `onclick="window.location.href='1stpage.html?var1=5&var2=6';"` you would write something like this: `onclick="goURL();"`, being `goURL()` a javascript function doing what you want. – Julio María Meca Hansen Apr 19 '13 at 07:56
  • Julio Meca Hansen Sir,i tried implementing the same `window.location.href='1st page.html?var1=5&var2=6';` and tried verifying it by printing the values on the second page... `console.log("value1: "+var1 +"value2: "+var2 );` alas!,i get this error...`E/Web Console(12476): ReferenceError: Can't find variable: var1 at file:///android_asset/www/1st%20page.html?var1=5&var2=6:17` – user Apr 19 '13 at 09:40
  • i am gonna use this method..till i get the output. :P – user Apr 19 '13 at 11:16
1

If you don't want to use javascript:

<a href="1stpage.html?var1=5&var2=6">
   <img src="images/img1.png" alt="" />
</a>
Sébastien Garmier
  • 1,263
  • 9
  • 16
  • lolcat111,i dont want the image src.....i just need to pass an array value from 1 html to another html page. – user Apr 18 '13 at 12:21
  • But the user must click on a link to get to the next page... The image is just decoration, you can also only put text in the link. – Sébastien Garmier Apr 18 '13 at 12:33
1

I have assumed you are using jquery but the principle is the same in plain old js - use a cookie e.g.

how to store an array in jquery cookie?

This actually stores the array rather than a load of variables as described in other answers.

Community
  • 1
  • 1
matpol
  • 3,042
  • 1
  • 15
  • 18
  • matpol,i am not using JQUERY...instead i am using javascript. – user Apr 18 '13 at 12:28
  • 1
    did you read the answer the principle is the same in plain old javascript 'set a cookie' (http://www.w3schools.com/js/js_cookies.asp) - don't mark me down for referencing w3schools but it was the first thing that came up on google! – matpol Apr 18 '13 at 12:31