Reputation: 119
Hi I am working with JsCookies it is working good but I am facing a problem
Whenever I store a short string in the cookies, it is working fine but the drawback is i am not able to store long strings in cookies
for example
This is working fine = Cookies.set('Coo', 'string', { expires: 1 })
But facing problem when ever i am trying to store stringified JSON data(large string)
Cookies.set('foo', JSON.stringify(result))
Please help
Upvotes: 3
Views: 2922
Reputation: 1916
You can't store more than 4k bytes in cookies but there is an alternative for this
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
In local storage, you can store up to 5MB of data so you can go with this, but the problem is if you want to set expiration you can't do it in a simple way, you need to make functions for that. like this
Upvotes: 1
Reputation: 1526
Use this function :
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
But there are limits to how large a cookie can be(4,096 bytes).
But you can have multiple cookies but in that too there are limits:
Chrome 9 allowed 180 cookies per domain
Firefox 3.6.3 allowed 50 cookies per domain
Internet Explorer 8 allowed 50 cookies per domain
Opera 10 and 9 allowed 30 cookies per domain
Upvotes: 0
Reputation: 370979
Cookies should not be used to store large amounts of data. They should be used for stuff like user authorization and configuration options (which, all together, don't add up to much of a payload). Use cookies when the data is relevant to both the client on the server on every request.
Large payloads in cookies should not be sent with every request; it'd be bandwidth-intensive and unnecessary. Instead, either:
Upvotes: 2
Reputation: 55866
Max cookie size is 4K bytes. You may not store more than that. Read more about cookie limits here: http://browsercookielimits.iain.guru/
If I run a test on my Chrome browser, here is what it says:
10:41:28.954: Guessing Max Cookie Count Per Domain: 180
10:41:28.954: Guessing Max Cookie Size Per Cookie: 4096 bytes
Upvotes: 0