firefox中Userdata Behavior和localStorage的getAttribute方法
getAttribute Method of Userdata Behavior and localStorage in firefox
我正在编写一个应该存储持久数据的网页。在IE中,我可以简单地使用UserdataBehavior来存储数据或对象。此外,UserdataBehavior提供了getAttribute和setAttribute方法来自定义存储的对象。
但firefox并不支持它。使用localStorage,我只能存储具有唯一密钥的字符串。我的问题是:是否可以使用类似的方法自定义存储在localStorage中的对象,如getAttribute或setAttribute?
我想通过使用localStorage将以下代码转换为一个新的代码,该代码应该在firefox下运行。但是我不知道如何转换setAttribute和getAttribute方法。知道吗?提前非常感谢。
<style type="text/css">
.storeuserData {
behavior: url(#default#userData);
</style>
<script type="text/javascript">
function fnSaveInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.setAttribute("sPersist",oPersist.value);
oPersist.save("oXMLBranch");
}
function fnLoadInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.load("oXMLBranch");
oPersist.value=oPersist.getAttribute("sPersist");
}
</script>
</head>
<body>
<form id="oPersistForm">
<input class="storeuserData" type="text" id="oPersistInput">
<input type="button" value="Load" onclick="fnLoadInput()">
<input type="button" value="Save" onclick="fnSaveInput()">
</form>
我对IE的实现不是很了解,但对于本地存储,你可以尝试这样的方法:
function setDataValue(attr, value){
//If local stoarge var doesn't exist, create it
if(typeof window.localStorage.mydata == 'undefined') window.localStorage.mydata = '{}';
//Read data from local storage in to object
var data = JSON.parse(window.localStorage.mydata);
//update object
data[attr] = value;
//save object back in to local storage
window.localStorage.mydata = JSON.stringify(data);
}
function getDataValue(attr){
return JSON.parse(window.localStorage.mydata)[attr];
}
基本上,上面所有的两个函数都是read&写入本地存储中存储的JavaScript对象(出于存储目的,编码为JSON)
你的函数可以写成:
function fnSaveInput(){
var oPersist=oPersistForm.oPersistInput;
setDataValue('sPersist', oPersist.value);
}
function fnLoadInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.value = getDataValue('sPersist');
}
请注意,所有这些代码都是我自己写的,所以可能会有一些打字错误。
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 使用Objective-C的JavaScript注入方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- toBoolean方法类似于toString
- 如何在单击复选框后调用控制器方法
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 递归使用 eval() 是检查程序执行的好方法吗?
- Java 脚本中的 getAttribute(attr) 方法返回属性的先前值
- firefox中Userdata Behavior和localStorage的getAttribute方法
- 对象没有't支持属性或方法'getAttribute'platform.js和IE11中出现错误
- ie9 getAttribute方法工作
- Internet Explorer 7元素中的错误.getAttribute方法