我项目中使用userData的实例 UserData.js

关于userData的介绍,请参见
http://hi.baidu.com/kaisep/blog/item/1da9a3312d2da5a15edf0e87.html
http://hi.baidu.com/kaisep/blog/item/bea532f58eb0082cbc310980.html
我这里直接说我的用法

我封装好的userData.js
document.write('<form id="oForm"><input style="DISPLAY: none; HEIGHT: 50px; WIDTH: 50px" ID="oInput"></form><IE:clientCaps id="ExculpateRate"></IE:clientCaps>')
var userData = oForm.oInput
//保存对象到XML文件中
// 如把select下拉列表的值0,以sSelectIndex为名称,保存到filename(将来在userdata中以filename[1].xml存在)中
//SaveUserData("sSelectIndex", filename, "0");
function SaveUserData(Main, Sub, strContent) {
try {
   userData.setAttribute(Main + userData.value, strContent)
   userData.save(Sub + userData.value)
} catch (e) {
}
}
//加载获取文件中的对象值
//如获取上面保存的值
//var str_bm = LoadUserData("sSelectIndex", filename)
function LoadUserData(Main, Sub) {
try{
userData.load(Sub + userData.value)
var sTmp = userData.getAttribute(Main + userData.value)
if (sTmp == null)
   sTmp = ""
return sTmp
}catch(e){
   return ""
}
}


使用方法:
先写入样式
<STYLE>    
      .userData {behavior:url(#default#userdata);}    
</STYLE> 
再引入userData.js
<script src="../etc/js/userData.js"></script>

实例HTML页面
<html>
<head>
<title>无标题文档</title>
<STYLE>    
      .userData {behavior:url(#default#userdata);}    
</STYLE> 
</head>

<script src="userData.js"></script>


<body>

<p align="center">wupin_idNotEvent[1].xml会保存在C:Documents and SettingsAdministratorUserData下面的随机文件夹中,</p>

<select id="wupin_idNotEvent" name="wupin_id" >
<option value="0" selected>请选择您要使用的设备类型</option>
<option value="001">虹光扫描仪FB2600B</option>
<option value="002">虹光扫描仪AW800</option>
<option value="003">虹光扫描仪AW1000</option>
<option value="004">虹光扫描仪AV210C2</option>
</select>

<script>
//在用户点中除(请选择项 外的option时,保存用户选中的值)
var obj=document.getElementById("wupin_idNotEvent");
var filename="wupin_idNotEvent";//wupin_idNotEvent[1].xml
var defaultIndex="0";
var defauleValue="请选择您要使用的设备类型";
//添加onchange事件
obj.attachEvent('onchange',saveSelectedIndex);

//保存用户选中的值(第一行除外)
function saveSelectedIndex(){
if(obj.options[obj.selectedIndex].value!="0"){
   //obj.setAttribute("sSelectIndex",obj.selectedIndex);
   //obj.setAttribute("sSelectValue",obj.options[obj.selectedIndex].value);
   SaveUserData("sSelectIndex", filename, obj.selectedIndex);
   SaveUserData("sSelectValue", filename, obj.options[obj.selectedIndex].value);
   alert("write ok");
}
}

//从userdata中 默认加载用户最后一次选择的值
window.attachEvent('onload',loadSelectedIndex)
function loadSelectedIndex(){
var str = LoadUserData("sSelectIndex", filename)
var str2 = LoadUserData("sSelectValue", filename)
//加载时如果为空就先保存
if (str == "") {
   SaveUserData("sSelectIndex", filename, "0");
   SaveUserData("sSelectValue", filename, "请选择您要使用的设备类型");
}
//取userdata中的值
var selectindex=LoadUserData("sSelectIndex", filename); 
var selectvalue= LoadUserData("sSelectValue", filename);
//var fpdm = LoadUserData("sSelectValue", filename);
// 循环,重新创建select,判断新的select是否有XML保存的 项。
var s = document.getElementById("wupin_idNotEvent"); //获取select标记
var ops= s.options; //获取对select标记options的引用
//有了才加载
for(var i=0; i<s.length; i++){
     if(selectvalue==ops[i].value && ops[i].value!="请选择您要使用的设备类型"){
       alert("加载上一次 的为选中状态");
       obj.options[selectindex].selected=true;
     }
   } 
}

</script>
</body>

</html>

原文地址:https://www.cnblogs.com/jijm123/p/8433989.html