Don'不再显示此页面(JQUERY cookie)

Don't show this page anymore (JQUERY cookie)

本文关键字:JQUERY cookie 不再 显示 Don      更新时间:2023-09-26

我正在尝试为我的网站实现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>