0

What other ways can i use to store small pieces of information than cookies?

Im building a widget that uses an iframe to load certain content. Within that iframe i want to save different states of the application, but i am getting problem when the widget is not on the same domain as the content inside the iframe (99% of the time).

Any ideas?

Oh and this is jQuery.

Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • There is localStorage as a better way to save local data but it won't help you solve cross-domain problems. You should probably use messaging to communicate between your frames of different domains. – Denys Séguret Feb 05 '13 at 07:54
  • You could try localStorage : http://en.wikipedia.org/wiki/Web_storage#localStorage and https://developer.mozilla.org/en-US/docs/DOM/Storage. It has some browser version restrictions though. Does not work with older browsers. – Ravi Y Feb 05 '13 at 07:54
  • Also check out http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – rkulla Feb 05 '13 at 07:56
  • Save them on server side database? – Ray Cheng Feb 05 '13 at 07:56
  • If i save them server side, i would just send and receive the data with jsonp? – Alosyius Feb 05 '13 at 08:05

3 Answers3

0

other than Jquery you can use
1. hashtags to save the state
2.HTML5 History API
3.HTML5 localstorage

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

Cookies are the way to go if you want to save persistent data for the specific user, however you can use HTML5, the difference being that the data stored with HTML is not explicitly shared with the server unless you use HTML5's sessionStorage. (session storage uses the same API but stores data specifically against the user session and is not persistent beyond session close);

Saving state with the HTML5 API is actually very easy, it is also event driven meaning that an event is fired whenever data is modified allowing you to listen for changes and act accordingly within your javascript / jQuery.

localStorage.name = 'David';

would create a variable within the localStorage called name, with the value David. Close your browser, re-open and console.log the variable, you will notice it persists.

Be wary that localStorage, despite previous documentation will only store strings of data and objects must be serialised and de-serialised if you want to store / retrieve them.

The actual spec is much larger and you can read more about it here:

HTML5 Storage API

Using HTML5 storage

Tips and tricks on using HTML5 storage

If you are not looking for persistant data you can use jQuery's .data() methods to store arbitrary data against a DOM nodefor later use. This is very useful, certainly if you are developing widgets and need to store non persistent state against a DOM node.

Additional:

ie7 and ie8 support is lacking but you can use a framework like modernizr.js to force legacy browser compatibility with HTML 5.

David Barker
  • 14,484
  • 3
  • 48
  • 77
0

I don't know why everyone is telling you to just to use HTML5 localstorage instead. Your issue is one of security.

Please see my comment above, for the community wiki link. Also see https://www.owasp.org/index.php/HTML5_Security_Cheat_Sheet and http://dev.w3.org/html5/webstorage/#security-localStorage

rkulla
  • 2,494
  • 1
  • 18
  • 16