How to manipulate cookie of array, object and json datatype using jQuery Plugins?

jquery_logo

A Cookie is a small amount of key value-based browser storage used to store tiny data on user’s browser by websites. It can be manipulated by JavaScript

Simple JavaScript code that manipulate cookie is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
document.cookie = "key=value; expires=EXPIRY_TIME";
document.cookie = "key=value; expires=EXPIRY_TIME";
document.cookie = "key=value; expires=EXPIRY_TIME";

There are many jQuery plugins available to implement cookie easily with simple callbacks. That provides methods for add, remove or delete browser cookies.

  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','value');
    $.cookie('key','value');
    $.cookie('key','value');
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key',['value1','value2'])
    $.cookie('key',['value1','value2'])
    $.cookie('key',['value1','value2'])
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','{
    "sub_key_1":"sub_value_1",
    "sub_key_2":"sub_value_2"
    }'
    );
    $.cookie('key','{ "sub_key_1":"sub_value_1", "sub_key_2":"sub_value_2" }' );
    $.cookie('key','{
                      "sub_key_1":"sub_value_1",
                      "sub_key_2":"sub_value_2"
                    }'
            );
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    // this cookie expires after 5 days
    $.cookie('key','value',{expires: 5});
    // this cookie expires after 5 days $.cookie('key','value',{expires: 5});
    // this cookie expires after 5 days
    $.cookie('key','value',{expires: 5});
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key',['value1','value2'], { expires: 5 });
    $.cookie('key',['value1','value2'], { expires: 5 });
    $.cookie('key',['value1','value2'], { expires: 5 });
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','{
    "sub_key_1":"sub_value_1",
    "sub_key_2":"sub_value_2"
    }'
    , {
    expires: 5
    }
    );
    $.cookie('key','{ "sub_key_1":"sub_value_1", "sub_key_2":"sub_value_2" }' , { expires: 5 } );
    $.cookie('key','{
                      "sub_key_1":"sub_value_1",
                      "sub_key_2":"sub_value_2"
                    }'
                  , {
                    expires: 5
                    }          
            );
  • More options

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','value'
    , {
    secure: true
    }
    );
    $.cookie('key','value' , { secure: true } );
    $.cookie('key','value'
                  , {
    		secure: true
                    }          
            );

    cross_domain_cookie

    Just prexif your domain value with dot (i.e. “.yourdomain.com”) in cookie option

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','value'
    , {
    secure: true
    }
    );
    $.cookie('key','value' , { secure: true } );
    $.cookie('key','value'
                  , {
    				secure: true
                    }          
            );

    So above cookie is valid for all subdomain of site vikaskbh.com Check demo here

    Path defines scope of cookie. By default, cookies are being set to directory structure of url. i.e. if you are accessing http://www.example.com/directory and try to set cookie, it will be set to /directory path. You could set it to root directory path “/” with cookie path option.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key','value'
    , {
    path: '/'
    }
    );
    $.cookie('key','value' , { path: '/' } );
    $.cookie('key','value'
                  , {
    				path: '/'
                    }          
            );
  • Reading cookie is simple, just pass cookie key to $.cookie() function.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key');
    $.cookie('key');
    $.cookie('key');
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookie('key').split(",");
    $.cookie('key').split(",");
    $.cookie('key').split(",");
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.parseJSON($.cookie('key'));
    $.parseJSON($.cookie('key'));
    $.parseJSON($.cookie('key'));
  • To delete cookie, just use function $.removeCookie() and supply cookie key.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.removeCookie('key');
    $.removeCookie('key');
    $.removeCookie('key');
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookies.set('key');
    $.cookies.set('key');
    $.cookies.set('key');
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookies.set('key',{
    sub_key:'sub_value'
    });
    $.cookies.set('key',{ sub_key:'sub_value' });
    $.cookies.set('key',{
    	sub_key:'sub_value'
    });
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookies.get('key');
    $.cookies.get('key');
    $.cookies.get('key');
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    $.cookies.del('key');
    $.cookies.del('key');
    $.cookies.del('key');
Total
0
Shares
Previous Post

How to sort HTML table rows using different jQuery plugins?

Next Post

GeForce GTX graphics card DVI-VGA display converter doesn’t work – FIX

Related Posts