-2
當用戶點擊一個按鈕時,我必須創建一個JS cookie,這個cookie會在另一個彈出窗口10分鐘後記住他。創建cookie Javascript
例子:
<button>Click me!</button>
和按鈕將隱藏在用戶點擊,10分鐘後該按鈕會再次顯示:
<button>Click me!</button>
腳本部分:
function setCookieMsg(name) {
var d = new Date();
var time = d.setTime(d.getTime() + (600000));
document.cookie = name + "=" + time;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
function checkCookieMsg() {
var cookie = getCookie("name");
var d = new Date() - 600000;
if (cookie == "") {
setCookie("name", cookie);
}else if (d => cookie) {
$().getUnReadMessage();
}
}
我錯了嗎?
我不知道你錯了什麼。你有什麼錯?它不起作用嗎?會發生什麼呢?你在瀏覽器的錯誤控制檯中看到錯誤嗎? – JJJ 2014-10-03 08:53:18
localStorage呢? – 2014-10-03 08:53:29
嘗試https://github.com/carhartl/jquery-cookie – user733421 2014-10-03 08:57:25