【javascript】js本地保存数据的一个解决方案:localStorage

‘localStorage‘ : localData = {hname : location.hostname ? location.hostname+‘/marchsoft/index.php/OA/‘ : ‘localStatus‘,isLocalStorage : window.localStorage ? true : false,dataDom : null,initDom:function(){ //初始化userDataif(!this.dataDom){try{this.dataDom = document.createElement(‘input‘);//这里使用hidden的input元素this.dataDom.type
= ‘hidden‘;this.dataDom.style.display = "none";this.dataDom.addBehavior(‘#default#userData‘);//这是userData的语法document.body.appendChild(this.dataDom);var exDate = new Date();exDate = exDate.getDate()+30;this.dataDom.expires = exDate.toUTCString();//设定过期时间}catch(ex){return
false;}}return true;},set:function(key,value){if(this.isLocalStorage){window.localStorage.setItem(key,value);}else{if(this.initDom()){this.dataDom.load(this.hname);this.dataDom.setAttribute(key,value);this.dataDom.save(this.hname)}}},get:function(key){if(this.isLocalStorage){return
window.localStorage.getItem(key);}else{if(this.initDom()){this.dataDom.load(this.hname);return this.dataDom.getAttribute(key);}}},add:function(key,value){ var before = this.get(key); if(before != null){ var add = true; var i = 0; for(i;i<before.length;++i){
if(value == this.get(key)[i]){ add = false; } } if(add){ this.set(key,new Array(this.get(key),value)); } }else{ this.set(key,new Array(value)); }},remove:function(key){if(this.isLocalStorage){localStorage.removeItem(key);}else{if(this.initDom()){this.dataDom.load(this.hname);this.dataDom.removeAttribute(key);this.dataDom.save(this.hname);}}}}

时间: 2024-08-05 10:02:18

【javascript】js本地保存数据的一个解决方案:localStorage的相关文章

JS本地保存数据的几种方法

1.Cookie 这个恐怕是最常见也是用得最多的技术了,也是比较古老的技术了.COOKIE优点很多,使用起来很方便 但它的缺点也很多: 比如跨域访问问题:无法保存太大的数据(最大仅为4KB):本地保存的数据会发送给服务器,浪费带宽 等等: 2.使用sessionStorage.localStorage localStorage: 是一种你不主动清除它,它会一直将存储数据存储在客户端的存储方式,即使你关闭了客户端(浏览器),属于本地持久层储存 sessionStorage: 用于本地存储一个会话(

js 本地保存 json/txt 文件

function download(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); if (document.createEvent) { var event = document

js本地图片预览,兼容ie[6-9]、火狐、Chrome17+、Opera11+、Maxthon3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> </head> <body> <div id="divPrevie

js本地存储解决方案(localStorage与userData)

WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的userData,Firefox下面的globalStorage,以及Flash的本地存储,除了Flash之外,其他的几个都有一些兼容性的问题. sessionStorage与localStorage Web Storage实际上由两部分组成:sessionStorage与localStorage. ses

JS解析Json 数据并跳转到一个新页面,取消A 标签跳转

JS解析Json 数据并跳转到一个新页面,代码如下 $.getJSON("http://api.cn.abb.com/common/api/staff/employee/" + obj.id, function (result) { window.open("https://abb-my.sharepoint.com/_layouts/15/me.aspx?p=" + result.Email, "_blank") }); 取消A 标签跳转 &l

如何在 javascript / js 中 建立一个map

建立map的方式(其实用的是json实现方式) var a = {}; a["key1"] = "value1"; a["key2"] = "value2"; 既然是个map就有检索某个键是否存在的方法,这样写 if ("key1" in a) { // something } else { // something else } 简单的一句话声明map里面的key和value的方式: var a = {'

javascript(js)小数精度丢失的解决方案

原因:js按照2进制来处理小数的加减乘除,在arg1的基础上 将arg2的精度进行扩展或逆扩展匹配,所以会出现如下情况. javascript(js)的小数点加减乘除问题,是一个js的bug如0.3*1 = 0.2999999999等,下面列出可以完美求出相应精度的四种js算法 function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(".")[1].length}catch(e){} t

Cocos2d JS 之消灭星星(十一) 本地保存玩家信息

当玩家退去游戏后,下次进入游戏可以接着上一次的游戏进度继续游戏; 1 /* 2 * win7下本地存储玩家的数据 3 */ 4 var PlayerLocalData = {}; 5 /* 6 * 玩家数据结构 7 */ 8 var playerData = function() 9 { 10 var playerD = [ 11 { 12 currentLevel:1, //玩家关卡 13 gameScore:0, //游戏得分 14 maxScore:0 //游戏最高得分 15 }]; 16

Android 分享一个SharedPreferences的工具类,方便保存数据

我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPreferences的使用也很简单,我自己就写了一个SharedPreferences的工具类,然后就保存在这里,等自己以后需要保存数据直接从这里copy代码,哈哈 工具类如下 [java] view plaincopy package com.example.shortcut; import androi