設置cookie: ...
設置cookie:
function setCookie(name, value, expTime){ var exp = new Date(); expTime = expTime ? expTime : 365; exp.setTime(exp.getTime() + expTime* 24 * 60 * 60 * 1000); //有效期 document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); //cookie名和值 }
獲取cookie:
function getCookie(name){ var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null; }
多站點不同功能變數名稱共用cookie:
比如有a.xxx.com 和 b.xxx.com,可設置在xxx.com,這樣a和b兩個站點就能共用cookie
function setCookie(name, value, expTime) { var exp = new Date(); expTime = expTime ? expTime : 365; exp.setTime(exp.getTime() + expTime * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/;domain=.xxx.com"; }