DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js增刪讀改cookie的方法
js增刪讀改cookie的方法
編輯:關於JavaScript     
//獲得cookie 的值
function cookie(name) {

var cookieArray = document.cookie.split("; "); //得到分割的cookie名值對

var cookie = new Object();

for (var i = 0; i < cookieArray.length; i++) {

var arr = cookieArray[i].split("="); //將名和值分開

if (arr[0] == name) return unescape(arr[1]); //如果是指定的cookie,則返回它的值

}

return "";

}



function delCookie(name)//刪除cookie
{

document.cookie = name + "=;expires=" + (new Date(0)).toGMTString();

}



function getCookie(objName) {//獲取指定名稱的cookie的值

var arrStr = document.cookie.split("; ");

for (var i = 0; i < arrStr.length; i++) {

var temp = arrStr[i].split("=");

if (temp[0] == objName) return unescape(temp[1]);

}

}



function addCookie(objName, objValue, objHours) { //添加cookie

var str = objName + "=" + escape(objValue);

if (objHours > 0) { //為時不設定過期時間,浏覽器關閉時cookie自動消失

var date = new Date();

var ms = objHours * 3600 * 1000;

date.setTime(date.getTime() + ms);

str += "; expires=" + date.toGMTString();

}

document.cookie = str;
}



function SetCookie(name, value)//兩個參數,一個是cookie的名子,一個是值
{
var Days = 30; //此 cookie 將被保存 30 天

var exp = new Date(); //new Date("December 31, 9998");

exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);

document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved