Skip to content Skip to sidebar Skip to footer

How To Pass A Variable's Value To The Next Page With JavaScript?

I want to be able to pass D, I, H, and A to the next page. My code on that page needs to be able to access their values. Here's what I have tried so far: function tally() { var

Solution 1:

In test1.html you write

localStorage.setItem("Dpoint", D); //passing this value to next page
localStorage.setItem("Ipoint", I); //passing this value to next page
localStorage.setItem("Hpoint", H); //passing this value to next page
localStorage.setItem("Apoint", A); //passing this value to next page

and in test2.html you get the values like this

D = localStorage.getItem("Dpoint"); 
I = localStorage.getItem("Ipoint");
H = localStorage.getItem("Hpoint"); 
A = localStorage.getItem("Apoint"); 

Note: This works only in modern browsers that support HTML5 local storage. To make it more robust and work across all browsers you can use some library like YUI which has a module called cache-offline. If you want to implement it yourself you can check the YUI code here

From security standpoint localstorage and cookies are the far more secure than URL GET params since these enforce the same domain restriction. If your data is more sensitive then you might want to consider it storing in the server side in some encrypted format.

From persistence standpoint this method only persists data within a browser. If you want cross browser persistence then again you need to think storing the info in the server.


Post a Comment for "How To Pass A Variable's Value To The Next Page With JavaScript?"