GithubHelp home page GithubHelp logo

cloudhead / store.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcuswestin/store.js

95.0 9.0 19.0 118 KB

localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood

Home Page: http://marcuswest.in

License: Other

JavaScript 100.00%

store.js's Introduction

store.js

store.js exposes a simple API for cross browser local storage

// Store 'marcus' at 'username'
store.set('username', 'marcus')

// Get 'username'
store.get('username')

// Remove 'username'
store.remove('username')

// Clear all keys
store.clear()

// Store an object literal - store.js uses JSON.stringify under the hood
store.set('user', { name: 'marcus', likes: 'javascript' })

// Get the stored object - store.js uses JSON.parse under the hood
var user = store.get('user')
alert(user.name + ' likes ' + user.likes)

store.js depends on JSON for serialization.

How does it works?

store.js uses localStorage when available, and falls back on globalStorage for earlier versions of Firefox and the userData behavior in IE6 and IE7. No flash to slow down your page load. No cookies to fatten your network requests.

Serialization

localStorage calls toString on all values that get stores. This means that you can't conveniently store and retrieve numbers, objects or arrays:

localStorage.myage = 24
localStorage.myage != 24
localStorage.myage == '24'

localStorage.user = { name: 'marcus', likes: 'javascript' }
localStorage.user == "[object Object]"

localStorage.tags = ['javascript', 'localStorage', 'store.js']
localStorage.tags.length == 32
localStorage.tags == "javascript,localStorage,store.js"

What we want (and get with store.js) is

store.set('myage', 24)
store.get('myage', 24) == 24

store.set('user', { name: 'marcus', likes: 'javascript' })
alert("Hi my name is " + store.get('user').name + "!")

store.set('tags', ['javascript', 'localStorage', 'store.js'])
alert("We've got " + store.get('tags').length + " tags here")

The native serialization engine of javascript is JSON. Rather than leaving it up to you to serialize and deserialize your values, store.js uses JSON.stringify() and JSON.parse() on each call to store.set() and store.get(), respectively.

Some browsers do not have native support for JSON. For those browsers you should include JSON.js (non-minified copy is included in this repo).

Tests

Go to test.html in your browser

Supported browsers

  • Tested in Firefox 2.0
  • Tested in Firefox 3.0
  • Tested in Firefox 3.5
  • Tested in Firefox 3.6
  • Tested in Chrome 5
  • Tested in Safari 4
  • Tested in Safari 5
  • Tested in IE6
  • Tested in IE7
  • Tested in IE8
  • Tested in Opera 10
    • Opera 10.54

Supported but borken (please help fix)

  • Chrome 4
  • Opera 10.10

Unsuported browsers

  • Firefox 1.0: no means (beside cookies and flash)
  • Safari 2: no means (beside cookies and flash)
  • Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
  • Opera 9: don't know if there is synchronous api for storing data locally
  • Firefox 1.5: don't know if there is synchronous api for storing data locally

TODO

  • I believe underlying APIs can throw under certain conditions. Where do we need try/catch?
  • Test different versions of Opera 10.X explicitly

store.js's People

Contributors

marcuswestin avatar mjpizz avatar

Stargazers

ZhengFang avatar iy88 avatar Alan Stocco avatar Cat  avatar professor avatar Emmanuel Iziren avatar Francois-Guillaume Ribreau avatar Romain Baumier avatar 阿太 avatar JYRICE avatar Angus H. avatar 斌上帝工作室 avatar Cxeep avatar  avatar  avatar Li Zhen Cheng avatar blue0125 avatar jim avatar Bob avatar wangcong avatar  avatar libin avatar Fenix avatar tedYHY avatar wulin avatar xinde avatar bugknightyyp avatar Kadir Atesoglu avatar  avatar Jarrick avatar jeffrey gaboni avatar steei avatar Spencer Thornock avatar etowner avatar  avatar Raphael S. E. avatar  avatar Chris Cai avatar Stefan avatar Moved to @ad-si avatar Nation avatar Kedar Amladi avatar Sergii Danilov avatar Kamil Zięba avatar Dinesh Kr. Choudhary avatar André Mendonça avatar Damián Eiff avatar  avatar anyforever avatar Pankaj Doharey avatar  avatar G_will avatar guixue avatar R. Mathieu Landvreugd avatar  avatar Radu Topala avatar Domenic Denicola avatar Ben Keith avatar  avatar Mathias Maisberger avatar Barnaby Malet avatar Chris Olstrom avatar Gustavo Verdun avatar Gilbert Seilheimer avatar Moses Yeung avatar Felipe Sayão Lobato Abreu avatar Henry avatar Valerii Iatsko avatar thinkphper avatar Adam Yanalunas avatar ᴄɪᴘʀɪᴀɴ ɪᴏɴᴇsᴄᴜ avatar  avatar Gerson Alexander Pardo Gamez avatar Mitch Dempsey avatar  avatar Maiz avatar Andrew-David J. avatar Michael I Angerman avatar Andy Beeching avatar  avatar Allex avatar Rob Sutherland avatar Matt Baily avatar Matthew M. Nelson avatar Ramin B. avatar Dino Reić avatar Thomas Brewer avatar Alexis Sellier avatar Nicolas Rudas avatar yuiseki avatar Ian Jorgensen avatar Cezar Sá Espinola avatar Andrzej Śliwa avatar Josh Brewer avatar

Watchers

Saggi Malachi avatar wangxian avatar James Cloos avatar Sergii Danilov avatar  avatar Emmanuel Iziren avatar zendwang avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.