使用Javascript进行基于Cookie的重定向

时间:2017-12-07 01:18:22

标签: javascript jquery html cookies

我试图根据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

&#13;
&#13;
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;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

的index.html

&#13;
&#13;
<!-- 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>
&#13;
&#13;
&#13;

landing.html上

&#13;
&#13;
<!-- 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>
&#13;
&#13;
&#13;

预期的结果是检查Cookie,然后将其转发到我的目标网页(如果尚未定义)。关于我做错了什么的想法?

1 个答案:

答案 0 :(得分:0)

当你从函数null

返回undefined时,你应该与null而不是get_cookie进行比较

的index.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>