con*_*led 68 html javascript cookies
检查cookie是否存在的好方法是什么?
条件:
Cookie存在,如果
cookie1=;cookie1=345534;
//or
cookie1=345534;cookie1=;
//or
cookie1=345534;
Run Code Online (Sandbox Code Playgroud)
如果,Cookie不存在
cookie=;
//or
<blank>
Run Code Online (Sandbox Code Playgroud)
jac*_*jac 109
您可以使用所需的cookie名称调用函数getCookie,然后检查它是否为null.
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("MyCookie");
if (myCookie == null) {
// do cookie doesn't exist stuff;
}
else {
// do cookie exists stuff
}
}
Run Code Online (Sandbox Code Playgroud)
heg*_*mon 72
我制作了一个替代的非jQuery版本:
document.cookie.match(/^(.*;)?\s*MyCookie\s*=\s*[^;]+(.*)?$/)
Run Code Online (Sandbox Code Playgroud)
它只测试cookie的存在.更复杂的版本也可以返回cookie值:
value_or_null = (document.cookie.match(/^(?:.*;)?\s*MyCookie\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1]
Run Code Online (Sandbox Code Playgroud)
将您的cookie名称替换为MyCookie.
Hac*_*ell 23
document.cookie.indexOf('cookie_name=');
Run Code Online (Sandbox Code Playgroud)
-1如果该cookie不存在,它将返回.
ps它的唯一缺点是(如评论中所述),如果存在具有此类名称的cookie,则会出错: any_prefix_cookie_name
(来源)
Dus*_*ead 11
这是一个老问题,但这是我使用的方法......
function getCookie(name) {
var match = document.cookie.match(RegExp('(?:^|;\\s*)' + name + '=([^;]*)'));
return match ? match[1] : null;
}
Run Code Online (Sandbox Code Playgroud)
这将null在 cookie 不存在或不包含请求的名称时返回。
否则,返回(请求名称的)值。
没有值的 cookie 永远不应该存在——因为,平心而论,那有什么意义呢?
如果不再需要它,最好一起摆脱它。
function deleteCookie(name) {
document.cookie = name +"=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}
Run Code Online (Sandbox Code Playgroud)
注意!所选答案包含一个错误(Jac的回答).
如果你有多个cookie(非常可能..)并且你正在检索的cookie是列表中的第一个,它不会设置变量"end",因此它将返回"cookieName"后面的整个字符串="在document.cookie字符串中!
这是该函数的修订版本:
function getCookie( name ) {
var dc,
prefix,
begin,
end;
dc = document.cookie;
prefix = name + "=";
begin = dc.indexOf("; " + prefix);
end = dc.length; // default to end of the string
// found, and not in first position
if (begin !== -1) {
// exclude the "; "
begin += 2;
} else {
//see if cookie is in first position
begin = dc.indexOf(prefix);
// not found at all or found as a portion of another cookie name
if (begin === -1 || begin !== 0 ) return null;
}
// if we find a ";" somewhere after the prefix position then "end" is that position,
// otherwise it defaults to the end of the string
if (dc.indexOf(";", begin) !== -1) {
end = dc.indexOf(";", begin);
}
return decodeURI(dc.substring(begin + prefix.length, end) ).replace(/\"/g, '');
}
Run Code Online (Sandbox Code Playgroud)
如果您正在使用jQuery,则可以使用jquery.cookie插件.
获取特定cookie的值如下:
$.cookie('MyCookie'); // Returns the cookie value
Run Code Online (Sandbox Code Playgroud)
请注意,如果 cookie 是安全的,则您无法使用document.cookie(所有答案都在使用)在客户端检查其存在。此类 cookie 只能在服务器端进行检查。
正则对象。test ( String )比 string快。匹配(正则表达式)。
所述MDN站点描述的document.cookie的格式,并且具有正则表达式示例抓住一个cookie( document.cookie.replace(/(?:(?:^|.*;\s*)test2\s*\=\s*([^;]*).*$)|^.*$/, "$1");)。基于此,我会这样做:
/^(.*;)?\s*cookie1\s*=/.test(document.cookie);
Run Code Online (Sandbox Code Playgroud)
该问题似乎要求一种解决方案,该解决方案在设置 cookie 时返回 false,但为空。在这种情况下:
/^(.*;)?\s*cookie1\s*=\s*[^;]/.test(document.cookie);
Run Code Online (Sandbox Code Playgroud)
测试
/^(.*;)?\s*cookie1\s*=/.test(document.cookie);
Run Code Online (Sandbox Code Playgroud)