一个简单的UserData管理器,可以save、load和delete数据。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> UserData manager </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="emu">
<META NAME="Keywords" CONTENT="IE USER DATA manager">
<META NAME="Description" CONTENT="UserData manager">
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.documentElement.addBehavior("#default#userdata");
function saveUserData(key, value){
var ex;
with(document.documentElement)try {
load(key);
setAttribute("value", value);
save(key);
return getAttribute("value");
}
catch (ex){alert(ex.message)}
}
function loadUserData(key){
var ex;
with(document.documentElement)try{
load(key);
return getAttribute("value");
}
catch (ex){alert(ex.message);return null;}
}
function deleteUserData(key){
var ex;
with(document.documentElement)try{
load(key);
expires = new Date(315532799000).toUTCString();
save(key);
}
catch (ex){alert(ex.message);}
}
saveUserData("emu","hello world !")
alert(loadUserData("emu"))
deleteUserData("emu")
alert(loadUserData("emu"))
//-->
</SCRIPT>
</BODY>
</HTML>
注意这一行: expires = new Date(315532799000).toUTCString();
315532799000 是格林威治时间1979年12月31日23时59分59秒。这是删除UserData的最靠前的一个有效expires时间了,再往前一毫秒,expires = new Date(315532798999).toUTCString(); 就删不掉userdata了,可以认为是IE的一个bug。所以我们删除userdata的时候不能像删除cookie一样,用new Date(0)来制造过期时间了。
一个等效的写法是 expires = new Date(1980,0,1,7,59,59).toUTCString(); 用toGMTString也一样。
如果需要按照user来管理userdata,可以这样:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> UserData manager </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="emu">
<META NAME="Keywords" CONTENT="IE USER DATA manager">
<META NAME="Description" CONTENT="UserData manager">
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.documentElement.addBehavior("#default#userdata");
function saveUserData(user,key,value){
var ex;
with(document.documentElement)try {
load(user);
setAttribute(key, value);
save(user);
return getAttribute("value");
}
catch (ex){alert(ex.message)}
}
function loadUserData(user,key){
var ex;
with(document.documentElement)try{
load(user);
return getAttribute(key);
}
catch (ex){alert(ex.message);return null;}
}
function deleteUserData(user){
var ex;
with(document.documentElement)try{
load(user);
expires = new Date(315532799000).toGMTString();
save(user);
}
catch (ex){alert(ex.message);}
}
saveUserData("emu","Name","emu !")
alert(loadUserData("emu","Name"))
saveUserData("emu","blog","http://www.blogjava.net/emu")
alert(loadUserData("emu","blog"))
//-->
</SCRIPT>
</BODY>
</HTML>
兼容上面两种的写法是这样:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> UserData manager </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="emu">
<META NAME="Keywords" CONTENT="IE USER DATA manager">
<META NAME="Description" CONTENT="UserData manager">
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.documentElement.addBehavior("#default#userdata");
function saveUserData(user,key,value){
var ex;
if(!value){
value = key;key=user;user="defaultUser"
}
with(document.documentElement)try {
load(user);
expires = new Date(new Date()-(-86400000)).toGMTString();
setAttribute(key, value);
save(user);
return getAttribute("value");
}
catch (ex){alert(ex)}
}
function loadUserData(user,key){
if(!key){
key=user;user="defaultUser";
}
var ex;
with(document.documentElement)try{
load(user);
return getAttribute(key);
}
catch (ex){alert(ex.message);return null;}
}
function deleteUserData(user){
var ex;
if(!user)user="defaultUser";
with(document.documentElement)try{
load(user);
expires = new Date(new Date()-86400000).toGMTString();
save(user);
}
catch (ex){alert(ex.message);}
}
saveUserData("emu","Name","emu !")
alert(loadUserData("emu","Name"))
deleteUserData("emu")
alert(loadUserData("emu","Name"))
saveUserData("emu","blog","http://www.blogjava.net/emu")
alert(loadUserData("emu","blog"))
deleteUserData("emu")
alert(loadUserData("emu","blog"))
saveUserData("emu_articles","http://www.blogjava.net/emu/category/1383.html")
alert(loadUserData("emu_articles"))
deleteUserData()
alert(loadUserData("emu_articles"))
//-->
</SCRIPT>
</BODY>
</HTML>