[英]firebase cloud function won't store cookie named other than "__session"
[英]Function won't store cookie
我正在尝试创建然后在JavaScript中检索Cookie。 我使用的是Google Chrome浏览器,当我运行下面的代码时,页面重新加载后不会存储cookie。
下面是我的代码
<html>
<title> Cookies </title>
<body onload="checkCookie()">
<script type="text/javascript">
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.lenth;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
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 checkCookie(){
var username = getCookie("username");
if(username!=null && username!=""){
alert("Welcome again " + username);
}else{
username=prompt("Please enter your name:","");
if(username!=null && username!=""){
setCookie("username",username,365);
document.write(username);
}
}
}
</script>
<body>
我不确定为什么它不起作用,我从这里从字面上复制/粘贴了它http://www.w3schools.com/js/js_cookies.asp
嗯,这是一些调试的有趣之处。 谢谢W3Schools 。 下次我建议您使用控制台和MDN
相当容易看到的第一个错误是:
c_end = c_value.lenth; //
c_end = c_value.length;//
如果使用该代码,则会注意到c is not defined.
控制台警报c is not defined.
好的,我们一个接一个地运行函数,我们看到运行getCookie
时出现错误
function getCookie(){
//Weird use if getIndex
//Someone forgot about regex
//..
c_end = c.value.length; // AHA!!!
}
c_end = c_value.length;
至于你的其他问题
setCookie
使用new date()
作为有效期,这意味着立即
采用:
function setCookie(c_name,value,exdays){
var exdate = exdays;
/....
}
请享用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.