不需要的双cookie安装

时间:2014-05-16 17:56:13

标签: javascript jquery html cookies

我的Cookie代码:

$(function() {
 $("#background").on('submit', function(e) {
    var src = $(this).find("input").val();
    $.cookie("background", src, {expires: 4*7});
 });
 $(".clear").click( function() {
    $.cookie("background",'');
    $("body").css("background-image", 'url('+$.cookie("background")+')');
 });
 $("body").css("background-image", 'url('+$.cookie("background")+')');
});

我在我的(en)主页和/ nl上使用它。 cookie工作正常,但是当我在(en)主页上安装cookie时,它将设置cookie,当我转到/ nl页面时,它将不会使用已安​​装的cookie,但你需要再次设置另一个,所以它不会使用主页的cookie,但会使用自己的cookie。

html代码:

<form id="background">
    <input type="text" />
</form>
<button class="clear">clear</button>

1 个答案:

答案 0 :(得分:0)

一种方法可以是:

//check if cookie already exists
if($.cookie('background')) {
//Clear the already existing cookie, here you can use 
$.removeCookie(‘background’) 
}

然后创建一个新的cookie,当您在2个不同的URL之间切换时,它将起作用。