<input id="txt1" type="text" /> <input id="txt2" type="text" /> <input id="btn1" type="button" />
//設置cookie function setCookie(name, value, iDay) { var oDate=new Date(); oDate.setDate(oDate.getDate()+iDay); document.cookie=name+'='+value+';expires='+oDate; } //獲取cookie function getCookie(name) { //'username=abc; password=123456; aaa=123; bbb=4r4er' var arr=document.cookie.split('; '); var i=0; //arr->['username=abc', 'password=123456', ...] for(i=0;i<arr.length;i++) { //arr2->['username', 'abc'] var arr2=arr[i].split('='); if(arr2[0]==name) { return arr2[1]; } } return ''; } //刪除cookie function removeCookie(name) { setCookie(name, '1', -1); } //cookie的調用 window.onload=function(){ var oTxt=document.getElementById("txt1"); var oTxt2=document.getElementById("pass"); var oBtn=document.getElementById("btn1"); oBtn.onclick=function(){ setCookie("name",oTxt.value,30); setCookie("name2",oTxt2.value,30) } oTxt.value=getCookie("name"); oTxt2.value=getCookie("name2"); }