2017-06-22 54 views
0

簡單的cookie記錄,我有7個複選框,kp1-kp7 setCookies,每次點擊它們中的一些時,都會創建cookie。問題是,當我關閉瀏覽器並重新打開頁面(本地)時,Cookie消失,它們不在那裏。 當我點擊複選框,並創建的cookie,他創建picture to prove itCookie始終過期instanlty

part of html code

window.onload = function(){ 

    if(document.cookie.length != 0){ 
    var nizKolacica = document.cookie.split("; "); 
    var nizVrednostiKolacica = nizKolacica.split("="); 

    // 0 - c1tip 
    // 1 - tekst/cb 
    // 2 - c2tip 
    // 3 - tekst/cb 

    if(nizVrednostiKolacica[1] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp1").checked = true; 
    }else{ 
     document.getElementById("kp1").checked = false; 
    } 

    if(nizVrednostiKolacica[3] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp2").checked = true; 
    }else{ 
     document.getElementById("kp2").checked = false; 
    } 

    if(nizVrednostiKolacica[5] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp3").checked = true; 
    }else{ 
     document.getElementById("kp3").checked = false; 
    } 

    if(nizVrednostiKolacica[7] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp4").checked = true; 
    }else{ 
     document.getElementById("kp4").checked = false; 
    } 

    if(nizVrednostiKolacica[9] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp5").checked = true; 
    }else{ 
     document.getElementById("kp5").checked = false; 
    } 

    if(nizVrednostiKolacica[12] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp6").checked = true; 
    }else{ 
     document.getElementById("kp6").checked = false; 
    } 

    if(nizVrednostiKolacica[12] == "tekst" || nizVrednostiKolacica.length == 0){ 
     document.getElementById("kp7").checked = true; 
    }else{ 
     document.getElementById("kp7").checked = false; 
    } 

    } 
} 

var kp1 = false; 
var kp2 = false; 
var kp3 = false; 
var kp4 = false; 
var kp5 = false; 
var kp6 = false; 
var kp7 = false; 

function kp1SetCoockie(){ 

    kp1 = !kp1; 
    if(kp1){ 
     document.cookie = "c1tip=tekst;expires=Sun, 25 Jun 2017 8:00:00 UTC"; 
    }else{ 
     document.cookie ="c1tip=cb"; 
    } 
} 

function kp2SetCoockie(){ 
    kp2 = !kp2; 
    if(kp2){ 
    document.cookie = "c2tip=tekst;max-age=3600"; 
    }else{ 
    document.cookie ="c2tip=cb"; 
    } 
} 

function kp3SetCoockie(){ 
    kp3 = !kp3; 
    if(kp3){ 
    document.cookie = "c3tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c3tip=cb"; 
    } 
} 

function kp4SetCoockie(){ 
    kp4 = !kp4; 
    if(kp4){ 
    document.cookie = "c4tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c4tip=cb"; 
    } 
} 

function kp5SetCoockie(){ 
    kp5 = !kp5; 
    if(kp5){ 
    document.cookie = "c5tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c5tip=cb"; 
    } 
} 

function kp6SetCoockie(){ 
    kp6 = !kp6; 
    if(kp6){ 
    document.cookie = "c6tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c6tip=cb"; 
    } 
} 

function kp7SetCoockie(){ 
    kp7 = !kp7; 
    if(kp7){ 
    document.cookie = "c7tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c7tip=cb"; 
    } 
} 

回答

0

您需要定義一個到期日期

var date = new Date(); 
    date.setTime(date.getTime() + (days*24*60*60*1000)); 
    var expires = date.toUTCString(); 
    document.cookie = name + "=" + value + "; expires=" + expires + "; path=/"; 

編輯:確實,這並不工作??

<html> 
    <head> 
     <script type="text/javascript"> 
      console.log(document.cookie); 

      var date = new Date(); 
      date.setTime(date.getTime() + (1*24*60*60*1000)); 
      var expires = date.toUTCString(); 

      console.log("test=test; expires=" + expires + "; path=/"); 
      document.cookie = "test=test; expires=" + expires + "; path=/"; 

      console.log(document.cookie); 
     </script> 
    </head> 
</html> 

我剛剛測試過,它工作。唯一沒有工作的是我的Firefox擴展,告訴我有0個cookie。但是當我用console.log(document.cookie);它給我印上了餅乾!

你的其他人在這裏應該做什麼?

if(kp3){ 
    document.cookie = "c3tip=tekst;max-age=3600; path=/"; 
    }else{ 
    document.cookie ="c3tip=cb"; 
    } 
+0

我做了kp1,但它不起作用 – Nosorog

+0

哦,我錯過了第一個......嗯,定義路徑? – Schlumpf

+0

嘗試過它在kp3:D – Nosorog