开发者

preserving the state using javascript and jquery

I having problem figuring howto preserve the present state ...Let say I have selected radio button and checkbox in present form and naviga开发者_运维知识库te way to a different page but if I want to go back to old page how should I able to see the selected radio and checkboxes in my previous page..


Well you can use cookies to do the same. here is a small code snippet that acts over cookies:

var myCookieHandler = (function () {
    return {
        createCookie: function (name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
        readCookie: function (name) {
            var nameEq = name + "=";
            var ca = document.cookie.split(';');
            var i;

            for (i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
                if (c.indexOf(nameEq) === 0) { return c.substring(nameEq.length, c.length); }
            }

            return null;
        },
        deleteCookie: function (name) {
            this.createCookie(name, null, -1);
        }
    };
}());

usage:

myCookieHandler .writeCookie("Login","true",2);

var cookieValue=myCookieHandler.readCookie("Login");

myCookieHandler.deleteCookie("Login");

Thus when you come back to this page you read your cookie and do the necessary with the same.

Hope this helps..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜