JQuery cookie plugin - multiple values?

I use the popular jquery cookie plugin https://github.com/carhartl/jquery-cookie I wonder how to set and read a cookie with multiple values? Or is it possible to add / remove values ​​for this cookie?

$. cookie ("MyTestCookie", email, {expires: 10});

I want to add the username to the same cookie

Update: Just an Example in .Net Saving Multiple Values ​​in Cookies

+8
jquery cookies
source share
3 answers

If you want to set a cookie with multiple values ​​or "subkeys" and read them with .NET, you need to save the subkey as name and name pairs, formatted as a query string. You can use the jQuery.param () method to convert a Javascript object to a query string.

var obj = { email: 'jdoe@example.com', username: 'jdoe' }; $.cookie("MyTestCookie", $.param(obj), { expires: 10 }); 

Then on the server you can access the values ​​as:

 var email = Request.Cookies["MyTestCookie"]["email"]; var username = Request.Cookies["MyTestCookie"]["username"]; 

EDIT: I created a test page to show reading / writing multicurrency cookies on both the server and client. http://www.systemex.net/Cookies/

NOTES:

  • You need to take care of shielding and disconnecting the connections. Thus, any inline = and and are handled correctly
  • When reading and writing jQuery cookies, use the {raw: true} parameter so that it does not double the escape code.
  • I wrote a function $ .deparam that converts the string name = value & name2 = value2 to a javascript object {name: value, name2: value2}
  • Recently, the jquery cookie plugin does not overwrite a cookie with the same name, it just adds it to the current cookie collection. At this point, it would probably be better to rewrite the plugin to support plugins and modify existing cookies.

Anyway, hope this helps.

Here is Default.aspx

 <h1>Get Cookie From Server:</h1> <ul> <li>Email: <%= GetCookie("MyTestCookie", "email")%></li> <li>Username: <%= GetCookie("MyTestCookie", "username")%></li> </ul> <h1>Get Cookie From Client:</h1> <ul> <li>Email: <span class="cookie" data-name="MyTestCookie" data-key="email" /></li> <li>Username: <span class="cookie" data-name="MyTestCookie" data-key="username" /></li> <li>Raw: <span id="raw" /></li> </ul> <h1>Set Cookie From Client:</h1> <ul> <li>Email: <input type="text" name="email" value="" /></li> <li>Username: <input type="text" name="username" value="" /></li> </ul> <input type="submit" value="Submit" /> <script> $(function () { $(".cookie").each(function (index) { var name = $(this).data('name'); var key = $(this).data('key'); var cookie = $.deparam($.cookie(name, { raw: true })); $(this).html(cookie[key]); }); $('#raw').text(escape($.cookie("MyTestCookie"), { raw: true })); $("form").submit(function () { var o = {}; o.email = $('input[name=email]').val(); o.username = $('input[name=username]').val(); var value = $.param(o); var cookie = $.cookie('MyTestCookie', value, { raw: true }); return true; }); }); jQuery.deparam = function (params) { var o = {}; if (!params) return o; var a = params.split('&'); for (var i = 0; i < a.length; i++) { var pair = a[i].split('='); o[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); } return o; } </script> 

Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { var cookie = new HttpCookie("MyTestCookie"); cookie["email"] = HttpUtility.UrlEncode("jdoe@example.com"); cookie["username"] = HttpUtility.UrlEncode("jdoe&123"); Response.Cookies.Add(cookie); } } public string GetCookie(string name, string key) { var cookie = Request.Cookies[name]; return cookie != null ? HttpUtility.UrlDecode(cookie[key]) : ""; } 
+17
source share

Even if this is not recommended, you can still add several values ​​to the cookie and do the processing yourself.

(This will reduce the number of cookies stored in the browser, but since then, modern browsers are designed to process huge piles of cookies would not do anything with download speed)

You can add your value using a loop and separate them with a special character, such as % ", and you can process your string using any encryption method you prefer and save everything as a single cookie.

 var setofcookies = username + "%" + password + "%" + email; $.cookie("MyTestCookie", setofcookies, { expires: 10 }); 

Later, you can use the SPLIT function to get the code into the correct array value:

 var cookie = $.cookie('MyTestCookie') var mycookies = cookie.split("%"); document.write(mycookies[0]); 

This is the most suitable method that you can use to process cookies, other methods slow down the page a bit.

+4
source share

Try the following: https://github.com/tantau-horia/jquery-SuperCookie

Quick use:

create - create cookie

check - check existence

verify - check cookie value if JSON

check_index - check if index exists in JSON

read_values ​​- read cookie value as a string

read_JSON - read cookie value as a JSON object

read_value - read the index value stored in the JSON object

replace_value - replace the value from the specified index stored in the JSON object

remove_value - remove the value and index stored in the JSON object

Just use:

 $.super_cookie().create("name_of_the_cookie",name_field_1:"value1",name_field_2:"value2"}); $.super_cookie().read_value("name_of_the_cookie","name_field_1"); 
+4
source share

All Articles