//Session类
function Session()
{
var SessionObj = null;
this.init = function()
{
SessionObj = document.createElement('input');
SessionObj.type = "hidden";
SessionObj.id = "Sessionid";
SessionObj.style.behavior = "url('#default#userData')"
document.body.appendChild(SessionObj);
}
this.load = function(sessionName)
{
if (sessionName != null && sessionName != "")
{
SessionObj.load("s");
return SessionObj.getAttribute(sessionName);
}
}
this.save = function(objId,attribute,sessionName)
{
var obj = null;
if (document.getElementById(objId) != null) obj = document.getElementById(objId)
else return;
var value = obj[attribute];
if (sessionName != null && sessionName != "")
{
SessionObj.setAttribute(sessionName,value)
SessionObj.save("s")
}
}
this.init();
}
var Session = new Session();
//例子:
在文本框中输入任意内容!!点击 [save] !!<br>
然后刷新页面!!也可以删除文本框内容!!! 点击[load] <br><br>
<input type="text" id="txt">
<input type="button" id="btn" value="save" onclick="Session.save('txt','value','op');">
<input type="button" id="btn1" onclick="alert(Session.load('op'));" value="load">
<script>
function Session()
{
var SessionObj = null;
this.init = function()
{
SessionObj = document.createElement('input');
SessionObj.type = "hidden";
SessionObj.id = "Sessionid";
SessionObj.style.behavior = "url('#default#userData')"
document.body.appendChild(SessionObj);
}
this.load = function(sessionName)
{
if (sessionName != null && sessionName != "")
{
SessionObj.load("s");
return SessionObj.getAttribute(sessionName);
}
}
this.save = function(objId,attribute,sessionName)
{
var obj = null;
if (document.getElementById(objId) != null) obj = document.getElementById(objId)
else return;
var value = obj[attribute];
if (sessionName != null && sessionName != "")
{
SessionObj.setAttribute(sessionName,value)
SessionObj.save("s")
}
}
this.init();
}
var Session = new Session();
</script>
该博客主要展示了用JavaScript实现的Session类代码。定义了Session类的初始化、加载和保存方法,通过创建隐藏的input元素来存储数据。还给出了使用示例,在文本框输入内容,点击保存和加载按钮可实现数据的存储与读取。
927

被折叠的 条评论
为什么被折叠?



