在Ie9 及以下可以使用,以上版本需要使用localstorage
参考文章
http://msdn.microsoft.com/zh-cn/vstudio/ms531424
过期
http://msdn.microsoft.com/zh-cn/vstudio/ms531095
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="gbk">
</head>
<body>
<input type="text" id="myInput">
<div id="write" οnclick="writea()">设置</div>
<div id="read" οnclick="reada()">读取</div>
</body>
<script type="text/javascript">
var mainObj = document.documentElement;
function writea() {
if (!mainObj.style.behavior) {
mainObj.style.behavior = 'url(#default#userData)';
}
var oTimeNow = new Date(); // Start Time
// 超时时间1分钟
oTimeNow.setMinutes(oTimeNow.getMinutes() + 1);
var sExpirationDate = oTimeNow.toUTCString();
mainObj.expires = sExpirationDate;
mainObj.setAttribute("MyData", document.getElementById("myInput").value);
mainObj.save("FirstUserDataStore");
}
function reada() {
try {
mainObj.load("FirstUserDataStore");
document.getElementById("myInput").value = mainObj.getAttribute("MyData");
}
catch (e) {
alert("没有数据 或者已经过期");
}
}
</script>
</html>