在 Javascript 中增加浏览器 cookie?(setCookie, getCookie method)
Increment browser cookie in Javascript? (setCookie, getCookie method)
从逻辑上讲,这似乎是正确的。但是,setCookie 或 getCookie 函数根本就没有触发?
饼干.js
function setCookie(c_name,value,exdays) { var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) +
((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; }
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
索引.php
var newCookie = parseInt(getCookie("liked_count"));
if(newCookie != null && newCookie != ""){
newCookie += 1;
setCookie("liked_count",newCookie,5);
}else{
setCookie("liked_count",1,5);
}
无论 if 语句的哪一边,无论如何都不会设置 cookie。据我所知,没有错误或警告,所以它是否在我的 cookie.js 文件中找不到 setCookie 和 getCookie 函数?
cookie.js文件成功定位,所以我在这里很聪明。
<head>
<script type="text/javascript" src="assets/js/cookies.js"></script>
</head>
任何帮助将不胜感激!
编辑:
哦,对不起,这很尴尬...事实证明,cookie.js文件正在缓存,我实际上已经移动了文件位置。就是这么简单。对不起,浪费时间!
您遇到的问题是使用:
var newCookie = parseInt(getCookie("liked_count"));
MDN parseInt 文档
parseInt 如果失败,则返回 NaN。而不是以下行:
if(newCookie != null && newCookie != ""){
你应该有
if(!isNaN(newCookie)) {
http://plnkr.co/edit/2Nj5pj
相关文章:
- PHP中的setcookie仅适用于localhost
- Javascript setcookie()
- 有人可以向我解释javascript getCookie()中while循环的功能吗?
- 在 Javascript 中增加浏览器 cookie?(setCookie, getCookie method)
- GetCookie returns NULL
- getCookie IE issue
- 关于JavaScript getCookie函数的说明
- SetCookiePhp and GetCookie Javascript
- javascript中的SetCookie在移动浏览器上不工作
- setCookie不能在Chrome中工作
- 如何管理getCookie功能和谷歌分析
- setcookie后的json数据不起作用
- 我可以返回文档的值吗?setcookie中的Getelementbyid
- PHP setcookie不工作与ajax调用