开发者

updating cookies with jquery

I'm trying to implement a side menu that saves it previous state (or selected item) after a submit or after the user refreshes the page.

I decided to use cookies to save the index of the selected item of the menu.

However, is not working everytime. What's going on?

Here's my code:

$(document).ready(function () {
            var cookie = $.cookie("SelectedNode");

            $('.t-link').click(function () {
                        var name = "SelectedNode";
                        var index = getIndex($(this));
                        $.cookie(name, null);   //delete previous value
                        $.cookie(name, index);
                        alert("It should save: " 开发者_运维百科+ index + " but it saved: " + $.cookie("SelectedNode"));
            });
});


I would go like this, I don't think you need to delete the cookie value, it just overrides the existing one.

$(document).ready(function () {
            var name = "SelectedNode";
            $('.t-link').click(function () {
                        var cookie = $.cookie(name, getIndex($(this)));
            });
});


Updating the same cookie with constantly evolving values is sometimes flaky depending on the browser you are using. I remember Firefox and Safari both being a PITA when trying to do this 2 years ago.

Anyway, you might consider a completely different tact and read/write the value into a hidden form field whose value is simply emitted back by your master page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜