我用兩個函數(也許原來的代碼是從here或here)用於獲取和設置的cookie,這裏有他們:
function setCookie(cookieName, content, expires, path) {
var date = new Date();
date.setDate(date.getDate() + expires);
var cookie = escape(content) + (expires == null ? "" : "; expires=" + date.toUTCString()) + (path != null ? "; path=" + path : "");
document.cookie = cookieName + "=" + cookie;
return true;
}
function getCookie(cookieName) {
var cookie = document.cookie,
begin = cookie.indexOf(" " + cookieName + "=");
if (begin == -1) begin = cookie.indexOf(cookieName + "=");
if (begin == -1) cookie = null;
else {
begin = cookie.indexOf("=", begin) + 1;
var end = cookie.indexOf(";", begin);
if (end == -1) end = cookie.length;
cookie = unescape(cookie.substring(begin, end));
}
return cookie;
}
隨着他們你可以輕鬆地做你想做的事情T:
- 處理頁面負載(例如
<body onload="pageLoad()">
)
- 添加腳本元件到頁的頭部部分,以及兩個funtions上述
- 添加腳本元件內的以下功能:
function pageLoad() { var cCont = getCookie('hitCount'); var count = 0; if (cCont != null) count = parseInt(count + ''); setCookie('hitCount', (count + 1) + '', null, null); }
- 如果你想得到命中計數,你可以使用
count
變量,或再次使用getCookie
函數。
你的第一個問題對我來說還不完全清楚,但是請閱讀this page,這裏有很好的例子和代碼示例。 This是Cookie的另一個很好的介紹。