Jquery Cookie plugin - multiple values?

后端 未结 4 515
长情又很酷
长情又很酷 2020-12-18 00:18

I am using popular jquery Cookie plugin https://github.com/carhartl/jquery-cookie Wonder how to set and read a cookie with multiple values? Or maybe it\'s possible to add/re

4条回答
  •  温柔的废话
    2020-12-18 01:03

    Even though it's not advised you still can add multiple values to a cookie and do the processing yourself.

    (This will reduce the amount of cookies kept in the browser but since modern browsers are designed to process huge loads of cookies it wouldn't do anything to the speed of loading)

    You can add your value using a loop and separate them using a special character like '%' and you can process your string on any encryption method you prefer and save all as one single cookie.

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

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

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

    This is the most appropriate method you can use to process cookies, other methods will slightly slow down the page.

提交回复
热议问题