admin管理员组文章数量:1323691
Is there a integer element for a cookie? I need the cookie to increment by 10 everytime a certain button is pushed. How can I start the cookie at 0 the first time and it's next value is based on it's current state.
document.cookie += 10;
gives 10, then 1010, then 101010.
Right I dea but I need integer values-
Need 10, 20, 30, etc...
Is there a integer element for a cookie? I need the cookie to increment by 10 everytime a certain button is pushed. How can I start the cookie at 0 the first time and it's next value is based on it's current state.
document.cookie += 10;
gives 10, then 1010, then 101010.
Right I dea but I need integer values-
Need 10, 20, 30, etc...
2 Answers
Reset to default 8document.cookie = parseInt(document.cookie) + 10;
Also, remember that you should never trust raw cookie data or data generated purely by javascript for anything serious as users can modify them as they see fit.
Altogether:
function sub_cookie()
{
if(!document.cookie)
document.cookie = 0;
document.cookie = parseInt(document.cookie) + 10;
//alert(document.cookie);
document.next.submit();
}
First time through the cookie is set to nothing, so it's initialized to 0. We can then continue incrementing the current state of the cookie, every time the function is called.
The parseInt() function parses a string and returns an integer.
本文标签: javascriptSetting a cookie value to a incrementing intStack Overflow
版权声明:本文标题:javascript - Setting a cookie value to a incrementing int - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135643a2422355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论