2013-12-21 4 views
-1

쿠키가 설정된 경우 환영 메시지를 표시하는 간단한 프로그램을 만들려고했는데 쿠키가 설정되지 않은 경우 사용자 이름을 묻는 메시지 상자가 표시됩니다.쿠키가 설정된 경우 시작 메시지 표시

작동하지 않으며 이유를 모르겠습니다. 아무도 코드에서 문제가 어디 있는지 말해 줄 수 있습니까? 코드에서이 오류가있어

<!DOCTYPE html> 
<html> 
<head> 
<script> 

function setCookie(c_name,value,expiredays){ 
    var exdate = new Date(); 
    exdate.setDate(exdate.getDate()+expiredays); 
    if(expiredays==null) 
     document.cookie = c_name + "=" + escape(value) +""; 
    else 
     document.cookie = c_name + "=" + escape(value) + ";expires=" 
     +exdate.toGMTString()); 
} 

function getCookie(c_name){ 
    if(document.cookie.length>0) 
    { 

     c_start=document.cookie.indexOf(c_name + "="); 
     if(c_start!=-1) 
      c_start=c_start + c_name.length +1; 
      //index of the value's end 
      c_end=document.cookie.indexOf(";",c_start); 
      if(c_end==-1)//if it's the last cookie 
       c_end = document.cookie.length; 
      return unescape (document.cookie.substring(c_start,c_end)); 
     } 
    } 
    return ""; 
} 

function checkCookie(){ 
    username=getCookie('username'); 
    if(username!=null && username!="") 
     alert('welcome again ' + username+ '!'); 
    else { 
     username=prompt('please enter your name:',""); 
     if(username!=null && username!="") 
      setCookie('username',username,365); 
     } 
} 
</script> 
</head> 
<body onload="checkCookie()"> 
</body> 
</html> 

http://jsbin.com/AcanusA/12/edit

+0

jsbin-tab "console"을 체크 아웃 했습니까? "예기치 않은 토큰)", "checkCookie가 정의되지 않았습니다" –

+0

왜 checkCookies가 정의되지 않았습니까? – Ohad

+0

@ Shiran 콘솔에 나와 있습니다! – Cilan

답변

3

: 여기

는 코드입니다. ( setCookie()의 기능이 마지막으로 toGMTString(); 인 경우) 및 하나의 "}"( getCookie() 기능의 경우, if(c_start!=-1){ 바로 뒤에있는) 중 하나가 닫히지 않습니다.

자바 스크립트 콘솔에서이 오류를 확인하십시오. 여기 보정입니다 :

<html> 
<head> 
<script> 

function setCookie(c_name,value,expiredays){ 
    var exdate = new Date(); 
    exdate.setDate(exdate.getDate()+expiredays); 
    if(expiredays==null) 
     document.cookie = c_name + "=" + escape(value) +""; 
    else 
     document.cookie = c_name + "=" + escape(value) + ";expires=" 
     +exdate.toGMTString(); 
} 

function getCookie(c_name){ 
    if(document.cookie.length>0) 
    { 

     c_start=document.cookie.indexOf(c_name + "="); 
     if(c_start!=-1){ 
      c_start=c_start + c_name.length +1; 
      //index of the value's end 
      c_end=document.cookie.indexOf(";",c_start); 
      if(c_end==-1)//if it's the last cookie 
       c_end = document.cookie.length; 
      return unescape (document.cookie.substring(c_start,c_end)); 
     } 
    } 
    return ""; 
} 

function checkCookie(){ 
    username=getCookie('username'); 
    if(username!=null && username!="") 
     alert('welcome again ' + username+ '!'); 
    else { 
     username=prompt('please enter your name:',""); 
     if(username!=null && username!="") 
      setCookie('username',username,365); 
     } 
} 
</script> 
</head> 
    <body onload="checkCookie()"> 
    za 
</body> 
</html> 
+0

재미있는 경고입니다 ... – Neikos

+0

예, 삭제했습니다. hehe – megatxi

+0

다음은 바이올린입니다. http://jsfiddle.net/UQTY2/230/ – sdespont

1

여기에 코드의 최적화 된 버전입니다 :

function getCookie(name) { 
    var setPos = document.cookie.indexOf(name + '='), stopPos = document.cookie.indexOf(';', setPos); 
    return !~setPos ? null : document.cookie.substring(
     setPos, ~stopPos ? stopPos : undefined).split('=')[1]; 
} 

function setCookie(name, val, days, path) { 
    var cookie = name + "=" + escape(val) + ""; 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
     cookie += ";expires=" + date.toGMTString(); 
    } 
    cookie += ";" + (path || "path=/"); 
    console.log(cookie); 
    document.cookie = cookie; 
} 

var username = getCookie("username"); 
if (!username) { 
    setCookie("username", prompt("please enter your name:"), 365); 
} else { 
    alert("welcome again " + username); 
} 

test case on jsFiddle를 참조하십시오.

관련 문제