2013-06-12 3 views
0

쿠키를 삭제하고 새로운 쿠키를 기능 (cookie_approve 내부)에서 설정하려고했지만 작동하지 않지만 기능 밖으로 나가면 쿠키가 삭제됩니다 .기능이 작동하지 않는 쿠키 삭제

의미 : 페이지에 div가 있습니다. 사용자가이 div 내의 버튼 (onClick 이벤트)을 클릭합니다. 쿠키가 생성됩니다. 다음 번에 사용자가 돌아올 때 (또는 사이트를 새로 고침)이 div는 표시되지 않습니다. 도움을

//create cookie 
function createCookie(name, value, days) { 
if (days) { 
    var date = new Date(); 
    date.setTime(date.getTime() + (10000 * 24 * 60 * 60 * 1000)); 
    var expires = "; expires=" + date.toUTCString(); 
} else var expires = ""; 
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/"; 
} 

//read cookie val 
function readCookie(name) { 
var nameEQ = escape(name) + "="; 
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, c.length); 
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length)); 
} 
return null; 
} 

//change cookie date 
function eraseCookie() 
{ 
document.cookie = 'cookie_approve=; expires='+new Date(0).toUTCString() +'; path=/'; 
} 

//hide div after click, create cookie with val=1 for 7 days 
function cookie_approve(){ 
eraseCookie(); 
createCookie("cookie_approve", 1, 7); 

document.getElementById('cookie_show_info').style.display = "none"; 
} 

if(readCookie("cookie_approve") == undefined){ 
createCookie("cookie_approve", 0, 7); 
} 

function show_cookie(){ 
    document.getElementById('cookies_rules').style.display = "block"; 
} 

function hide_cookie(){ 
document.getElementById('cookies_rules').style.display = "none"; 
} 

document.write('<div id="cookie_show_info" style="display: none;">Text <div id="cookies_rules">Cookie rules</div> <a onClick="cookie_approve()">Button</a></div>'); 

if(readCookie("cookie_approve") == 0){ 
document.getElementById("cookie_show_info").style.display = "block"; 
} 

감사 :

내 코드입니다.

+0

를 작동하지 않습니다? createCookie() 함수는 어떻게 생겼습니까? – abimelex

+0

createCookie 함수 내 코드 위에 있습니다. 이전에 언급했듯이 : 쿠키를 삭제하고 onClick 이벤트를 사용하여 새 val = 1로 다시 작성하려고합니다. – Petre

답변

0

귀하의 eraseCookie() 함수는

이상한 조금 어쩌면 그 시도 같습니다

function createCookie(name,value,days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

function eraseCookie(name) { 
    createCookie(name,"",-1); 
} 

출처 : 정확히 만들거나 삭제, 무엇을 http://www.quirksmode.org/js/cookies.html

+0

아니, 작동하지 않습니다. 전에 말했듯이 버튼 링크 (onclick 이벤트 사용)를 클릭했지만 쿠키는 변경되지 않습니다. Val = 0. 쿠키가 onClick 이벤트를 사용하여 변경 될 수없는 것처럼 보입니다. 가능한가? – Petre

+0

바이올린을 설정할 수 있습니까? 버튼은 다른 것입니까? preventDefault 속성을 설정 했습니까? – abimelex

+0

http://zabezpieczstrone.pl에서 찾을 수있는 모든 것. 페이지 하단의 분홍색 부분 :) Akceptuj는 버튼 (숨어서 val = 1로 쿠키 만들기)입니다. 내가 알아 차 렸던 것 : 디버거가 다음과 같은 것을 발견했습니다 : TypeError : document.cookie.split이 함수가 아닙니다. – Petre

관련 문제