Does anyone know how to use the Jquery cookie plugin to set the cookie value as an integer?
According to this page
Setting a cookie value to a incrementing int
There is a passInt Javascript functio开发者_如何学JAVAn which works like this:
document.cookie = parseInt(document.cookie) + 10;
and the normal way for setting a jquery cookie plugin variable is along the lines of
$.cookie("mySweetCookie", "1", { expires: 7 }, { path: '/' });
with 1 as a string. But like the stackoverflow mentioned above, I'm trying to do incremental integer increases and I'm too noob with jquery to figure out how to pass an integer.
Thanks ahead for any help
To my understanding, you can't pass an integer to a cookie, it gets converted to a string anyway. You have to get the value from the cookie, and then parseInt()
the string (number) that was returned, increment it and then pass it back to the cookie.
Like this:
$(document).ready(function () {
var cookie_name = "mySweetCookie";
var cookie_value = $.cookie(cookie_name);
cookie_value = parseInt(cookie_value);
$.cookie(cookie_name, ++cookie_value, { expires: 7 }, { path: '/' });
});
Hope this works.
精彩评论