【发布时间】:2013-01-06 07:19:34
【问题描述】:
我对 jQuery 很陌生,我正在尝试让 jQuery 插件 jquery.cookie.js 编写一个 cookie,然后根据 cookie 值进行重定向。以下是我要完成的工作的概要:
在登陆启动页面后,用户会选择他们的语言偏好。他们还可以选中“记住我”复选框,该复选框将 cookie lang-pref 写入值为en 或fr。以后访问时,访问者会被重定向到英文主页或法文主页。
这是我编写 cookie 的代码:
$(function()
{
$("#en").change(function() //#en is the id of the checkbox
{
if ($(this).is(":checked"))
$.cookie('mem', 'en', { expires: 3652 }); // mem is the cookie name, en is the value
})
});
这是读取 cookie 的代码,我有理由确定我搞砸了,因为重定向不起作用。不过,我不确定如何解决它:
$(function() {
if ($.cookie('mem'))
$(location).html ("window.location.href = 'http://www.mysite.com/home-en.php'");
});
我已经查看了这个插件的文档,但我仍然不确定如何使用实际 cookie 的值来执行操作:例如,on the project's GitHub page 给出的示例显示了如何读取 cookie,只是通过做我在上面的代码中所做的事情。
长话短说,我不知道如何读取 cookie 的 值,然后使用该值执行重定向。
【问题讨论】:
标签: jquery jquery-cookie