Javascript在不同的功能中创建Cookie
Javascript Creating a Cookie within a different funtion
我的问题是我试图调用一个函数来在不同的函数中创建cookie。由于某些原因,createCookie函数不会执行。
我调用createCookie(名称)的函数
function login() {
var user = document.getElementById('name').value;
var pass = document.getElementById('pass').value;
if(user='Jacob',pass='password') {
createCookie('name','Jacob',0);
}else{
alert('Invalid Credentials');
}
}
createCookie(name)函数
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
当我尝试使用这些函数查找值时,它显示为null。
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function display(){
var x = readCookie('name');
alert(x);
}
据我所见,您的代码存储和检索cookie很好,但您的代码中有一个大错误:
if(user='Jacob',pass='password') {
这意味着"将'Jacob'
分配给user
,将'password'
分配给pass
,然后生成值'password'
"(该值将始终被视为true
)。
您需要的是:
if(user === 'Jacob' && pass === 'password') {
另外,不要吝啬花括号。有很多它们可供使用,如果没有它们,您的代码将一团糟。以下操作似乎很好:
function login() {
var user = document.getElementById('name').value;
var pass = document.getElementById('pass').value;
if (user === 'Jacob' && pass === 'password') {
createCookie('name', 'Jacob', 0);
} else {
alert('Invalid Credentials');
}
}
function createCookie(key, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else {
var expires = "";
}
document.cookie = key + "=" + value + expires + "; path=/";
}
function readCookie(key) {
var keyEq = key + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function display() {
var x = readCookie('name');
alert(x);
}
还要注意,readCookie
可以替换为:
function readCookie(key) {
return document.cookie.split(';').map(function (item) {
return item.split('=').map(decodeURIComponent);
}).filter(function (item) {
return item[0] && item[0].trim() === key;
}).map(function (item) {
return item[1];
})[0];
}
下面的代码正在工作-
var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
createCookie('username','Jacob',0);
alert(getCookie('username'));
Fiddle Url-https://jsfiddle.net/zr6h8nf7/3/
相关文章:
- 在C#中创建cookie,并通过Javascript更改值
- Javascript在不同的功能中创建Cookie
- 可以在没有域名的情况下创建cookie吗
- 从javascript,向我的cookie添加域或过期不会创建cookie
- 在模态窗口上创建 cookie
- 表达,使用 JSON 创建 cookie
- 使用 Javascript 创建 Cookie 并设置过期时间
- 使用按钮 javascript 在 forloop 中创建 Cookie
- 在 PHP 中使用 js API 使用 evercookie 创建 cookie 的问题
- 为每个选项卡单独创建 cookie
- 如何使用外部域创建 cookie
- 在localhost上创建cookie
- asp.net/javascript创建cookie,创建客户端或服务器端的优势
- 用于创建cookie的Javascript代码
- Jquery+Javascript:使用toggle创建cookie
- 使用InvokeScript在Windows Phone中创建cookie
- 在文档准备好之前安全地创建cookie
- 获取JSESSIONID值并在AngularJS中创建Cookie
- 是否有任何方法手动创建cookie的Internet explorer
- 使用ionic angularjs创建cookie,并将cookie持久化很长时间