session.html

来自「javascript中也有自己的session机制」· HTML 代码 · 共 43 行

HTML
43
字号
<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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?