2012年5月4日 星期五

[HTML5] Web Local Storage


Here bring up some samples for local storage in html5.
Remember that all data is stored as String.
It is stored in sqlite data for Chrome browser, and you can find the sqlite data in folder
C:\Documents and Settings\(UserNmae)\Local Settings\Application Data\Google\Chrome\User Data\Default\Local Storage

    // to detect browser whether support local storage
    function supports_html5_storage() {
        return ('localStorage' in window) && window['localStorage'] !== null;
    }

    // to get and set local storage
    localStorage.setItem("bar1", "haha - foo1");
    var foo1 = localStorage.getItem("bar1");

    // to get and set local storage using square-bracket
    localStorage["bar2"] = "haha - foo2";
    var foo2 = localStorage["bar2"];

    // to remove specify key-value pair
    localStorage.removeItem("bar1");

    // to clear storage
    localStorage.clear();

    // to get total number of values in the storage area.
    var storageLength = localStorage.length;

    // to get the name of each key by index.
    var keyName = localStorage.key(0);


Reference:
[OReilly] HTML5 Up and Running

沒有留言:

張貼留言