2014-09-02 1 views
1

내 웹 사이트에 쿠키를 구현하려고합니다. 사용자를 스플래시 페이지로 리디렉션하고 "내 정보 기억"확인란을 선택한 다음 입력을 누르면 더 이상 해당 페이지가 표시되지 않습니다.이 페이지를 더 이상 표시하지 않습니다. (JQUERY 쿠키)

$(function() { 
    var COOKIE_NAME = 'splash-page-cookie'; 
    $go = $.cookie(COOKIE_NAME); 
    if ($go == null) { 
     $.cookie(COOKIE_NAME, 'test', { path: '/', expires: 6 }); 
     window.location = "/splash.php" 
    } 
    else { 
    } 
}); 

누구나이 전에 같이 수행했다 .. 어떻게 날 상자를 기억 감지 은 그래서 사용자를위한 쿠키를 설정하고 페이지로 리디렉션 할 수 COOKIE Plugin를 사용하여,하지만 난 구현 할 수 없습니다? 아니면 누구나 비슷한 아이디어를 구현할 수 있습니까?

도움을 주시면 감사하겠습니다.

답변

2

해결책 # 1 (경고 상자 포함) : COOKIE Plugin없이이 기능을 제안했습니다. 바라건대 당신은 이것에서 약간의 사용을 얻을 수 있습니다. 거의 Pure JS와 약간의 JQuery를 사용하여 조금 놀란다.

여기는 DEMO입니다.

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Untitled Document</title> 
<!-- JQuery --> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script> 
$(document).ready(function(e) { 
    $('#remember').click(function() { 
     if (this.checked) { 
      $('#x').text("You're the Best!"); 
      setCookie(); 
     } else { 
      $('#x').text("Come on, You know you want to!"); 
     } 
    }); 
}); 

function setCookie() { 
    user = prompt("Please enter your name:",""); 
    if (user != "" && user != null) { 
     addCookie("username", user, 30); 
    } 
} 

function addCookie(cname,cvalue,exdays) { 
    var d = new Date(); 
    d.setTime(d.getTime() + (exdays*24*60*60*1000)); 
    var expires = "expires=" + d.toGMTString(); 
    document.cookie = cname+"="+cvalue+"; "+expires; 
    //window.location.href = "https://www.google.com"; // redirect after the prompt 
} 

function getCookie(cname) { 
    var name = cname + "="; 
    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); 
     if (c.indexOf(name) != -1) { 
      return c.substring(name.length, c.length); 
     } 
    } 
    return ""; 
} 

function checkCookie() { 
    var user=getCookie("username"); 
    if (user != "") { 
     alert("Welcome again " + user); 
     window.location.href = "https://www.google.com"; 
    } 
} 
function goTo() { 
    window.location.href = "https://www.google.com"; 
} 
</script> 
</head> 
<body onLoad="checkCookie();"> 
<h1>Splash Page</h1> 
<form> 
<input type="checkbox" id="remember"> Remember me 
<br> 
<input type="button" value="Enter" onClick="goTo();"> 
</form> 
<p><div id='x'></div></p> 
</body> 
</html> 

솔루션 # 2 (NO 경고 상자) : 여기

코드의 난 (등, 크롬) 모바일 더 호환 요청에 의해 두 번째 단순화 된 솔루션이 함께했다 . 바라건대 당신은 이것에서 약간의 사용을 얻을 수 있습니다. 거의 Pure JS와 약간의 JQuery를 사용하여 조금 놀란다.

여기는 DEMO입니다. 그냥보고 .. 지연에 대한

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Untitled Document</title> 
<!-- JQuery --> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 
$(document).ready(function(e) { 
    $('#remember').click(function() { 
     if (this.checked) { 
      $('#x').text("You're the Best!!!"); 
      addCookie(30); 
     } else { 
      $('#x').text("Come on, You know you want to!"); 
      deleteAllCookies(); 
     } 
    }); 
}); 

function deleteAllCookies() { 
    var cookies = document.cookie.split(";"); 
    for (var i = 0; i < cookies.length; i++) { 
     var cookie = cookies[i]; 
     var eqPos = cookie.indexOf("="); 
     var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; 
     document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; 
    } 
} 

function addCookie(exdays) { 
    var d = new Date(); 
    d.setTime(d.getTime() + (exdays*24*60*60*1000)); 
    var expires = "expires=" + d.toGMTString(); 
    document.cookie = expires; 
} 

function checkCookie() { 
    if (document.cookie == false) { 
     //alert("Welcome New User"); 
    } else if (document.cookie.indexOf("expires") >= 0) { 
     //alert("Welcome Back"); 
     window.location.href = "https://www.google.com"; 
    } 
} 

function goTo() { 
     window.location.href = "https://www.google.com"; 
} 
</script> 
</head> 
<body onLoad="checkCookie();"> 
<h1>Splash Page</h1> 
<form> 
<input type="checkbox" id="remember"> Remember me 
<br> 
<input type="button" value="Enter" onClick="goTo();"> 
</form> 
<p><div id='x'></div></p> 
</body> 
</html> 
+0

미안 해요, 난 멀리 잠시 동안이고 이것은 우리가하라는 제거하고 단지 기본 이름을 가진 쿠키를 설정 얻을 수있는, 중대하다 : 여기

코드인가? – Amir

+0

신경 쓰지 마세요. – Amir

+0

Chrome/Android에서는 작동하지 않습니다. . 데스크톱 및 모바일 (Chrome, Safari, Opera, FF)의 다른 모든 브라우저에서 작동하는 경우 문제가 무엇인지 알 수 있습니까? – Amir

관련 문제