persist javascript variables across pages

persist javascript variables across pages  using -'Is,there,a,way,we,can,persist,javascript,variables,across,various,pages?,Suppose,in,Page,A,I,am,setting,window.someVar,=,5.,Then,I,move,to,Page,B,,via,clicking,a,hyperlink,in,A,,and,do,something,like,alert(window.someVar),--,I,should,get,a,message,box,displaying,5.,Is,there,a,technique,to,persist,someVar,as,such...?'

asked Oct 13, 2015 by sujata naik
edited Oct 14, 2015 by rajesh
0 votes

6 Answers

0 votes

You could use the window’s name to store the information. This is known as JavaScript session. But it only works as long as the same window/tab is used.

answered Oct 13, 2015 by sandeep bhadauria
0 votes

I would recommend you to give a look to this library:

I really like it, it supports a variety of storage backends (from cookies to HTML5 storage, Gears, Flash, and more...), its usage is really transparent, you don't have to know or care which backend is used the library will choose the right storage backend depending on the browser capabilities.

answered Oct 13, 2015 by 20shahi
0 votes

Yes, using Cookies. But be careful, don't put too much in them (I think there is a limit at 4kb). But a few variables are ok.

If you need to store considerably more than that, check out @Annie's great tips in the other answer. For small time data storage, I would say Cookies are the easiest thing.

Note that cookies are stored client side.

answered Oct 13, 2015 by suyesh.lokhande
0 votes

For completeness, also look into the local storage capabilities of HTML5. These are supported in the latest versions of all modern browsers, and are much easier to use and less fiddly than cookies.

answered Oct 13, 2015 by deepak
0 votes

You can use as a wrapper around localStorage. It stores complex objects but doesn't merely stringify and parse the whole thing like most such libraries do. That's really inefficient if you want to store a lot of data and add to it or change it in small chunks. Also, JSON discards a lot of important stuff like non-numerical properties of arrays.

In rhaboo you can write things like this:

var store = Rhaboo.persistent('Some name');

store.write('count', store.count ? store.count+1 : 1);

var laststamp = store.stamp ? store.stamp.toString() : "never";
store.write('stamp', new Date());

store.write('somethingfancy', {
  one: ['man', 'went'],
  2: 'mow',
  went: [  2, { mow: ['a', 'meadow' ] }, {}  ]

store.somethingfancy.went[1].mow.write(1, 'lawn');
console.log( store.somethingfancy.went[1].mow[1] ); //says lawn

BTW, I wrote rhaboo

answered Oct 13, 2015 by dahiyabecomp
0 votes

I recommend web storage. Example:

// Storing the data: localStorage.setItem("variableName","Text"); // Receiving the data: localStorage.getItem("variableName");

Just replace variable with your variable name and text with what you want to store. According to W3Schools, it's better than cookies.

answered Oct 13, 2015 by deepak gupta