【问题标题】:Cookie based Redirection using Javascript使用 Javascript 的基于 Cookie 的重定向
【发布时间】:2017-12-08 03:21:23
【问题描述】:

我正在尝试基于 cookie 的存在来创建重定向。所以当用户第一次连接到我的网站jonathanstevens.org时,他们会被重定向到jonathanstevens.org/landing

代码部分:

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

Global.js

function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime( date.getTime() + (days*24*60*60*1000) );
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie(name) {
    var nameEQ = name + "=";
    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, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

索引.html

<!-- redirect to landing page -->
<script>
  // Redirect to landing page if 'form_submitted' cookie does not exist
  if (get_cookie('secondvisit') === 'undefined') {
    window.location.href = "landing.html";
  }
</script>

登陆.html

<!-- Adds second Visit cookie -->
<script>
	jQuery(document).ready(function($) {
    // Create cookie so that the user is no longer redirected
    create_cookie('secondvisit', 'true', 30);
	});
</script>

预期结果是检查 cookie,如果未定义,则将我转发到我的登录页面。关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: javascript jquery html cookies


    【解决方案1】:

    当您从函数 get_cookie 返回 null 时,您应该与 null 而不是 undefined 进行比较

    索引.html

    <!-- redirect to landing page -->
    <script>
      // Redirect to landing page if 'form_submitted' cookie does not exist
      if (get_cookie('secondvisit') === null) {
        window.location.href = "landing.html";
      }
    </script>
    

    除此之外,您应该使用这个library 非常好的一个易于使用的工具,请参见下文

    创建一个在整个网站都有效的 cookie:

    Cookies.set('name', 'value');
    

    创建一个在 7 天后过期的 cookie,在整个网站上有效:

    Cookies.set('name', 'value', { expires: 7 });
    

    创建一个过期cookie,对当前页面的路径有效:

    Cookies.set('name', 'value', { expires: 7, path: '' });
    

    读取 cookie:

    Cookies.get('name'); // => 'value'
    Cookies.get('nothing'); // => undefined
    

    读取所有可见的 cookie:

    Cookies.get(); // => { name: 'value' }
    

    删除 cookie:

    Cookies.remove('name');
    

    删除对当前页面路径有效的cookie:

    Cookies.set('name', 'value', { path: '' });
    Cookies.remove('name'); // fail!
    Cookies.remove('name', { path: '' }); // removed! 
    

    编辑

    使用js.cookie 库的代码转换版本如下所示。

    (注意:我已经测试了这段代码,它可以正常工作,请确保您正确包含库并且控制台上没有错误。)

    Index.html

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
    <!-- redirect to landing page -->
    <script>
        $(document).ready(function () {
            if (typeof Cookies.get('secondvisit') === 'undefined') {
                window.location.href = "landing.html";
            }
        })
        // Redirect to landing page if 'form_submitted' cookie does not exist
    </script>
    

    landing.html

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
    <!-- Adds second Visit cookie -->
    <script>
        jQuery(document).ready(function () {
            // Create cookie so that the user is no longer redirected
            var a = Cookies.set('secondvisit', 'true', {
                expires: 7
            });
        });
    </script>
    

    【讨论】:

    • 谢谢。这使重定向工作。但是,当页面“登陆”加载时,它不会创建 cookie。因此,当我单击返回 index.html 时,它会再次重定向我。 (我在本地工作 chrome)。 &lt;script&gt; jQuery(document).ready(function($) { cookies.set('secondvisit', 'true'); }); &lt;/script&gt;我还包含了你建议的库。
    • 我现在不在,会回家看看我能做些什么来解决这个问题。
    • 除了它是Cookies.set() 而不是cookies.set() 所以这可能是它不适合你的原因,我已经添加了上面的代码。 @乔纳森史蒂文斯
    • 这是否适用于我的本地文件系统,或者我必须上传它。如果是这样,也许这就是问题所在......
    • 我正在寻找的优秀信息,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2013-11-26
    • 2015-05-30
    相关资源
    最近更新 更多