가능한 중복 : 나는 쿠키를 확인하고 20 분 이전인지 여부를 말할 필요로하는 프로젝트를 진행하고
What is the “best” way to get and set a single cookie value using JavaScriptJavaScript로 쿠키를 처리하는 방법은 무엇입니까?
. 그래서 나는 이렇게 한 번 코드를 작성했습니다. 이것은 붙여 넣은 자바 스크립트 코드입니다.
function checkcookie()
{
var difftime= getcookie();
// further operation comes here
}
var cookieminutes;
function getcookie()
{
var start = document.cookie.indexOf("expires");
var cookiedate;
if(start==-1)
{
cookiedate = new Date();
document.write("Start equal to -1");
document.cookie="expires="+cookiedate+",path=0,domain=0";
cookieminutes= cookiedate.getMinutes();
}
else
{
document.write("Start not equal to -1");
var date = new Date();
var minutes = date.getMinutes();
document.write("The difference is "+minutes);
document.write("<br />Cookie minutes is "+cookieminutes);
return (minutes-cookieminutes);
}
}
getcookie에서 변수 cookieminutes가 정의되지 않은 상태로 제공됩니다. 그러나 나는 그것이 글로벌 변수이기 때문에 그것이 가치를 가져야한다는 것을 알고있다.
아무에게도 해결책을 말해 줄 수 있습니까?
그것의 getCookie 주어진 페이지로드에서 처음 호출되는 경우처럼 보이는, 그리고 쿠키가 존재 cookieminutes가 설정되지 않습니다. –