Html代码: 复制代码 代码如下: JavaScript与Cookie(一) CookieMessage: Valid-time:Please input a integer. Js代码: 复制代码 代码如下: <BR>/* <BR>* 设置Cookie的通用函数,其中name是必须的参数。其它为可选,故用条件语句判断。 <BR>* 在设置Cookie时若不设置过期时间则该Cookie为临时的,仅当此次会话可用 <BR>*/ <BR>function setcookie(name, value, expires, path, domain, secure) { <BR>var curcookie = name + "=" + encodeURI(value) <BR>+((expires) ? ";expires=" + expires.toGMTString() : "") <BR>+((path) ? ";path=" + path : "") <BR>+((domain) ? ";domain=" + domain : "") <BR>+((secure) ? ";secure" : ""); <BR>document.cookie = curcookie; <BR>} <br><br>//Cookie的写入 <BR>function savecookie() { <BR>var cookieInfo = document.frm1.cookieInfo.value; <BR>var time = document.frm1.time.value; <BR>var now = new Date(); <BR>now.setDate( now.getDate() + time); <BR>setcookie("myCookie",cookieInfo,now); <BR>document.write(decodeURI(document.cookie)); <BR>} <br><br>//读取所有Cooke <BR>function writeAllCookie() { <BR>document.cookie = "name1=" + encodeURI("名字1"); <BR>document.cookie = "name2=" + encodeURI("名字2"); <BR>document.cookie = "name3=" + encodeURI("名字3"); <BR>var strInfo; <BR>var cookie = document.cookie.split(";"); <BR>for(var i=0; i<cookie.length; i++) { <BR>var ck = cookie[i].split("="); <BR>var cName = ck[0]; <BR>var cValue = decodeURI(ck[1]); <BR>strInfo += cName + "=" + cValue +"<br>"; <BR>} <BR>document.getElementById("divInfo").innerHTML = strInfo; <BR>} <br><br>/* <BR>* 读取特定Cookie的通用函数 <BR>*/ <BR>function getSpecificCookie(name) { <BR>if(document.cookie.length > 0) { <BR>start = document.cookie.indexOf(name + "="); <BR>if( start != -1) { <BR>start = start + name.length + 1; <BR>end = document.cookie.indexOf(";",start); <BR>if( end == -1) { <BR>end = document.cookie.length; <BR>} <BR>} <BR>return decodeURI(document.cookie.substring(start,end)); <BR>} <BR>return ""; <BR>} <br><br>function getCk(){ <BR>name = document.frm1.txtGetSpc.value; <BR>alert( getSpecificCookie(name)); <BR>} <BR>