使用JavaScript的基于Cookie的重定向 [英] Cookie based Redirection using Javascript
问题描述
我试图基于cookie存在创建重定向.因此,当用户首次连接到我的网站jonathanstevens.org
时,他们将被重定向到jonathanstevens.org/landing
I'm attempting to create a redirect based on a cookie existence. So when a user connects to my website jonathanstevens.org
for the first time, they are redirected to 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
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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Index.html
Index.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>
landing.html
landing.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,然后将我转发到我的登录页面(如果未定义).关于我做错了什么的任何想法?
The expected result was it to check for a cookie, then forward me to my landing page if it wasn't defined. Any ideas on what I've done wrong?
推荐答案
当您从函数get_cookie
Index.html
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>
除此之外,您应该使用这个图书馆确实是一个很好的工具,易于使用见下文
Apart from this you should use this library really good one an easy to work with see below
创建一个cookie,该cookie在整个网站上均有效:
Cookies.set('name', 'value');
创建一个从现在起7天到期的cookie,该cookie在整个网站上均有效:
Cookies.set('name', 'value', { expires: 7 });
创建一个有效的cookie,该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
库的代码转换后的版本如下所示.
A converted version of your code using js.cookie
library will look like following.
(注意:我已经测试了此代码,并且可以正常工作,请确保您正确包含了库,并且控制台上没有错误.)
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>
这篇关于使用JavaScript的基于Cookie的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!