zoukankan      html  css  js  c++  java
  • HTML5之Local Storage

    HTML5 STORAGE SUPPORT
    IEFIREFOXSAFARICHROMEOPERAIPHONEANDROID
    8.0+ 3.5+ 4.0+ 4.0+ 10.5+ 2.0+ 2.0+

    Local Storage Detection:

    ## 方法一
    function supports_local_storage() {
    try { return 'localStorage' in window && window['localStorage'] !== null; } catch(e){ return false; } }
    ## 方法二 
    if(localStorage && localStorage.getItem('LOCALSTORAGEKEY')){
      // localStorage is available and has the KEY 'LOCALSTORAGEKEY'
    }
    if (Modernizr.localstorage) {
      // localStorage is available!
    } else {
      // no native support for local storage :(
      // try a fallback or another third-party solution
    }

    Local Storage Method:

    key:

    e.g: localStorage.key(1)

    getItem:

    e.g: localStorage.getItem('LOCALSTORAGEKEY')

    setItem:

    e.g: localStorage.setItem('LOCALSTORAGEKEY', 'LOCALSTORAGEVALUE')

    removeItem:

    e.g: localStorage.removeItem('LOCALSTORAGEKEY')

    clear:

    e.g: localStorage.clear()

    Tracking changes:

    if (window.addEventListener) {
      window.addEventListener("storage", handle_storage, false);
    } else {
      window.attachEvent("onstorage", handle_storage);
    }
    
    function handle_storage(e) {
      if (!e) { e = window.event; }
    }

    STORAGEEVENT OBJECT:

    PROPERTYTYPEDESCRIPTION
    key string the named key that was added, removed, or modified
    oldValue any the previous value (now overwritten), or null if a new item was added
    newValue any the new value, or null if an item was removed
    url* string the page which called a method that triggered this change
    * Note: the url property was originally called uri. Some browsers shipped with that property before the specification changed. For maximum compatibility, you should check whether the url property exists, and if not, check for the uri property instead.

    注意事项:

    1、不推荐使用window.localStorage,可直接使用localStorage,window.localStorage在IE下有可能会抛出错误;

    2、localStorage是以key=value值对形式存在的,value值一律为字符串,字面量对象可用JSON.stringify()转换成字符串后赋值给key;

    3、localStorage.getItem('KEY')取出key对应的值后,需要JSON.parse()进行解析,否则其值只是一个看起来像字面量对象的字符串,同⓶;

    4、如果通过localStorage.key(num)取值,num的起始索引是0,不推荐;

    5、localStorage存储上限为5MB,超出会抛出“QUOTA_EXCEEDED_ERR”;

  • 相关阅读:
    2020牛客暑期多校第三场B-Classical String Problem(字符串移动思维)
    2020牛客暑期多校第四场B-Basic Gcd Problem(思维+数论)
    2020牛客暑期多校第三场E-Two Matchings(规律DP)
    2020牛客暑期多校第三场C-Operation Love(计算几何-顺逆时针的判断)
    odoo高级物流应用:跨厂区生产
    Odoo车辆管理
    安装odoo 9实录
    Odoo 养猪
    【转】结转本年利润的有关分录
    Odoo POS
  • 原文地址:https://www.cnblogs.com/sirius-cg/p/4591831.html
Copyright © 2011-2022 走看看