【问题标题】:Don't show this page anymore (JQUERY cookie)不再显示此页面(JQUERY cookie)
【发布时间】:2014-09-02 15:40:05
【问题描述】:

我正在尝试为我的网站实施 cookie,我只是想将用户重定向到启动页面并在那里有一个“记住我”复选框,一旦他们勾选并按下回车,他们将不再看到该页面. 所以使用COOKIE Plugin我可以为用户设置一个cookie并将他们重定向到页面,但我无法实现如何检测remember me box..

$(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 {
    }
});

以前有人这样做过吗?或者任何人有任何类似的想法来实现这个?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript php jquery cookies


    【解决方案1】:

    解决方案 #1(带有警告框):我想出了这个,但没有使用 COOKIE 插件。希望你能从中得到一些用处。主要是纯 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(无警告框):我根据要求提出了第二个简化的解决方案,它与移动设备(Chrome 等)更兼容。希望你能从中得到一些用处。主要是纯 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>
    

    【讨论】:

    • 抱歉耽搁了,我离开了一段时间.. 刚刚看到,这很好,我们可以摆脱提示并设置一个默认名称的 cookie 吗?
    • 它不适用于 Chrome/Android 。 .但是在桌面和移动设备(Chrome、Safari、Opera、FF)的所有其他浏览器中工作,您知道会出现什么问题吗?
    • @Amir 我希望我上面的第二个解决方案对你有用。这对我来说很好。
    • 嘿伙计,我真的很感激。 .还有一件事:我想检查是否设置了 cookie,所以我可以说是否设置了 cookie 加载页面,否则重定向到启动页面(您的代码) 我可以通过 PHP 来完成吗?还是我应该通过 JS 来做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2016-03-25
    • 1970-01-01
    • 2020-11-10
    • 2018-11-22
    相关资源
    最近更新 更多