Don'不再显示此页面(JQUERY cookie)
Don't show this page anymore (JQUERY cookie)
我正在尝试为我的网站实现cookie,我只是想将用户重定向到一个启动页面,并在那里设置一个"记住我"复选框,一旦他们勾选并按下回车键,他们就再也看不到该页面了。因此,使用COOKIE插件,我可以为用户设置COOKIE并将他们重定向到页面,但我无法实现如何检测"记住我"框。。
$(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 {
}
});
以前有人这样做过吗?或者有人有类似的想法来实现这一点?
如有任何帮助,我们将不胜感激。
解决方案#1(带警报框):我提出了这个,但没有COOKIE插件。希望你能从中得到一些用处。大部分是纯JS,还有一些JQuery,让它有点花哨。
这是演示。
这是代码:
<!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(无警报框):我根据请求提出了第二个简化的解决方案,它与移动设备(Chrome等)更兼容。希望你能从中得到一些用处。大部分是纯JS,还有一些JQuery,让它有点花哨。
这是演示。
这是代码:
<!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>
相关文章:
- 使用jquery cookie的第三方cookie
- Jquery cookie - toggle box
- 如何使用 jquery.cookie 创建 if 语句
- 基于 jQuery Cookie 打开和关闭面板
- jquery cookie,在清除浏览器历史记录后仍然设置
- 为什么jQuery.cookie插件返回“[object object]”
- jQuery可以't使用jquery.cookie插件读取cookie
- jQuery cookie is not expring
- 正在保存CSS State Jquery Cookie
- 如何使用jquery cookie插件保存toggleClass方法的状态
- JQuery Cookie在1分钟后过期
- JQuery Cookie 不起作用
- jQuery Cookie 插件许可证
- jquery cookie 通过循环追加值
- JQuery cookie 在其他函数中设置
- 使用函数setTimerOut和jquery Cookie的模型窗口
- jQuery Cookie插件保存选择值
- jquery cookie for jquery animate
- 为什么这个 JQuery Cookie 值没有更新
- 将对象/数组存储在 jQuery Cookie 中