在新窗口中记住我的jquery cookie值

时间:2012-07-26 12:15:10

标签: jquery cookies

不知道如何做到这一点,我在页面上的链接上设置了一个jQuery cookie,然后打开一个新窗口。因此,在窗口1上 - cookie值设置为XXX。窗口2是一个页面,用户可以将此cookie值更新为XYXYYY等任何内容。

所以我希望能够在窗口1中更新用户在窗口2中更改的值。

目前我有2个窗口,其中$.mycookie(test, xxx)$.mycookie(test, yyy)。如果用户关闭窗口2,则他的更改不会在窗口1中更新。

2 个答案:

答案 0 :(得分:1)

要明确使Cookie可用于域中的所有路径,请确保路径已设置:

$.cookie("example", "foo", { path: '/' });

将其限制为特定路径:

$.cookie("example", "foo", { path: '/foo' });

如果设置为“/”,则Cookie将在整个域中可用。如果设置为'/ foo /',则cookie只能在/ foo /目录和所有子目录中使用,例如/ foo / bar / of domain。默认值是设置cookie的当前目录。

答案 1 :(得分:0)

你是对的,你需要刷新开启窗口。

Window-1通过javascript打开Window-2(弹出窗口),这个弹出窗口需要有一个函数,当它关闭时和/或点击刷新父窗口的按钮/链接时触发了unload事件。以您执行此操作的方式发生的Cookie更改只能在刷新后激活。

简单窗口-2示例:

<script>
function whatever() {
    $.cookie("test", $('input[name=cookievalue]').val());
    if (window.opener != null) {
        window.opener.location.reload();
        window.close();
    }
}

$('button[name=clicker"]').bind('click', function(){
    whatever();
});

/* optionall you can also do an unload event, 
 * but you should probably check if you ran whatever() more then 
 * once and not run it a second time.
 */
$(window).unload(function(){ 
    whatever();
});
</script>

<input type="text" value="" name="cookievalue">
<button name="clicker">Change Cookie and close</button>