欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML5 本地存储 localStorage SessionStorage StorageListener- 2

程序员文章站 2022-09-25 12:44:22
%setion   %p= "localStorage"   %aside     %ul  ...

%setion
  %p= "localStorage"
  %aside
    %ul
      %li
        %button.add= "添加数据"
      %li
        %button.get= "获取数据"
      %li
        %button.remove= "移除数据"
      %li
        %button.click= "清除数据"
      %li
        %button.parse= "解析JSON数据"
    %section.localStorage

 

 

 

pie={};

pie.load = function(func){
  jQuery(function(){
    try{
      func();
    }catch(e){
      console.log("PIE: js加载错误 " + e);
    }
  });
}

 


pie.load ->
 
  $userData = {
    name:"Sankyu Name",
    account:"sankyu",
    level:1,
    disabled:true
  }

  localStorage.getItem("userData")
 
  jQuery('.get').on 'click', ->
    $localStorage = jQuery('.localStorage')
    $localStorage.html(localStorage.getItem("userData"))

  jQuery('.add').on 'click', ->
    $localStorage = jQuery('.localStorage')
    localStorage.setItem("userData",JSON.stringify($userData))
    $localStorage.html("增加成功")

  jQuery('.remove').on 'click', ->
    localStorage.removeItem("userData")
    $localStorage = jQuery('.localStorage')
    $localStorage.html("删除成功")

  jQuery('.click').on 'click', ->
    localStorage.clear
    $localStorage = jQuery('.localStorage')
    $localStorage.html("已清除")

  jQuery('.parse').on 'click', ->
    $parse = JSON.parse(localStorage.getItem("userData")).name
    $localStorage = jQuery('.localStorage')
    $localStorage.html($parse)

jQuery(window).on 'load', ->
    window.addEventListener("storage",
      ((e) ->
        console.log(e)
        $storage = jQuery('p .storage')
        $storage.html("删除成功")
      ),true)
    localStorage.clear
    localStorage.setItem("userData","storage demo")
    localStorage.setItem("userData", "storge event demo")