Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery-设置cookie和重定向_Javascript_Jquery_Html_Cookies - Fatal编程技术网

Javascript Jquery-设置cookie和重定向

Javascript Jquery-设置cookie和重定向,javascript,jquery,html,cookies,Javascript,Jquery,Html,Cookies,我在mysite.com/index.html上有一个站点,在mysite.com/test.html上有一个复制站点 我需要为访问mysite.com/test.html的访问者设置cookie,因此如果他在浏览器mysite.com中键入内容,他将在接下来的30天内重定向到mysite.com/test.html 我正在使用jquery.cookie.js插件 我在mysite.com/test.html中放置了以下代码: $.cookie('test', 'test', { expires

我在mysite.com/index.html上有一个站点,在mysite.com/test.html上有一个复制站点

我需要为访问mysite.com/test.html的访问者设置cookie,因此如果他在浏览器mysite.com中键入内容,他将在接下来的30天内重定向到mysite.com/test.html

我正在使用jquery.cookie.js插件

我在mysite.com/test.html中放置了以下代码:

$.cookie('test', 'test', { expires: 30, path: '/' })
在mysite.com/index.html上

$(function() {
    if ($.cookie('test')) window.location.href = 'http://www.mysite.com/test.html';
});
我对js和jquery非常陌生,所以请告诉我设置cookie、读取和重定向是否正确


谢谢。

您遇到了什么问题?@Ibu我想问一下这是否是一种正确的方法,因为我对jquery非常陌生,但我需要确保这能完成任务。谢谢。我不明白为什么不应该这样做,请在您的页面上尝试并亲自查看。顺便说一句,在服务器端执行重定向会更好,这将为您节省一个额外的请求。您可能更喜欢
window.location.replace
,请参阅@Ibu Can I Plate this'if(isset($\u COOKIE[“test”]){header(“location:”。$\u COOKIE[“test”])在html页面的head中进行服务器端重定向?谢谢你的回复。