Javascript Cookie проблемы IE - PullRequest
1 голос
/ 14 мая 2010

копил голову над каким-то Javascript, помогите, пожалуйста, я не понимаю, почему он просто не находит мой cookie в IE 7 или 8

Я устанавливаю cookie в true через другое событие, но я просто хочу, чтобы 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);

}

Ответы [ 2 ]

1 голос
/ 14 мая 2010

С чего начать ..

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 голосов
/ 14 мая 2010

Вы также можете сделать это с помощью регулярного выражения:

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