2010-05-14 75 views
1

被裝袋我的頭了一些JavaScript,請大家幫幫忙,我不能明白爲什麼它根本不會發現在IE 7中我的餅乾或8JavaScript Cookie進行廣告問題IE

我設置cookie真正通過另一個事件,但我只是希望看到IE拿起我最初設定的cookie。在Firefox中也可以,預先感謝。

var t=setTimeout("doAlert()",8000); 
var doAlertVar = true; 
document.cookie = "closed=0;expires=0;path="; 

function readCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie; 
    alert(ca); 
    ca = ca.replace(/^\s*|\s*$/g,''); 
    ca = document.cookie.split(';'); 

    for(var i=0;i < ca.length;i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

function doAlert(){ 

    if(readCookie('closed')==1){ 
     doAlertVar = false; 
    } 
    if(readCookie('closed')==0){ 
     alert("unlicensed demo version\nbuy online at"); 

    } 
    t=setTimeout("doAlert()",5000); 

} 
+0

你到底看到了什麼?什麼時候得到提醒和何時?你期望看到什麼? – Dan 2010-05-14 14:10:58

+0

我沒有看到我在Javascript的開始('closed = 0;')添加的cookie,只是那些可以通過http請求 – blakey87 2010-05-14 14:38:12

回答

1

從哪裏開始..

setTimeout("doAlert()",8000); 
// do not use strings as an argument to setTimeout, that runs eval under the hood. 
// use 
setTimeout(doAlert,8000); 
// instead 

document.cookie = "closed=0;expires=0;path="; 
// this is wrong, expires should follow the format Fri, 14 May 2010 17:22:33 GMT (new Date().toUTCString()) 
// path should be path=/ 
0

您還可以使用正則表達式做到這一點:

function readCookie(name, defaultValue) { 
    var value = defaultValue; 
    document.cookie.replace(new RegExp("\\b" + name + "=([^;]*)"), function(_, v) { 
    value = v; 
    }); 
    return value; 
}