What are the alternatives to html5 session storage?(not local storage)

7.2k views Asked by At

I want 2 javascript methods to access certain data without passing this data to any of the methods as arguments. Basically I want one method to set the data and other to consume that data using the objects already available in the browser and I want the solution to work with Safari, Firefox, Chrome and IE8+ also iOS and android browsers. I believe session storage does not work on iOS. Is it correct? What are the drawbacks of session storage. I tried to append the data to the event object but it does not work in firefox and IE.

3

There are 3 answers

0
Akshay Shinde On BEST ANSWER

I opted for data attributes instead of above mentioned approaches and they are working fine for me. IE9 and IE8 are presenting some issues which I need to tackle. In IE9 & IE8 the data attributes work sometimes and sometimes not. After I use the console to query data-attributes of the element, they start working.

3
Matt L On

You could use a "global variable" (I put that in quotes because I'm not sure what it's actually called in Javascript). But basically you could just make an object in one of your javascript files and write to/read from it.

Something like

var data = { first: "first", second: "second" }

and just make as many variables in that object as you need. And a function would access it like

function(param) {
    var firstData = data.first;
}

Or I guess using cookies is closer to what you described as your own solution, but that seems like overkill if you just need to pass data between Javascript methods.

2
Samuli Hakoniemi On

Although sessionStorage is available for iOS, you can do following for legacy support. You can use window.name. It lives through the session (i.e. until the browser tab gets closed):

var myData = {
  'foo': 'bar',
  'foz': 'baz'
};

window.name = JSON.stringify(myData);

And you can read it back:

var myData = JSON.parse(window.name);

This of course requires JSON support from the browser, which leaves very old browsers out of the scope.