jQuery 检查 Cookie 是否存在,如果不存在则创建它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6362688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery check if Cookie exists, if not create it
提问by ToddN
I cannot get this code to work I must be missing something pretty simple. I am trying to check to see if a Cookie exists, if it does {do nothing} if it doesn't {create it}. I am testing the cookie by including an alert on a page. Basically I do not want the cookie to keep re-creating with a referral url, I am trying to grab only the FIRST referred URL.
我无法让这段代码工作我一定错过了一些非常简单的东西。我正在尝试检查 Cookie 是否存在,如果它不{创建它},它是否{不做任何事}。我正在通过在页面上包含警报来测试 cookie。基本上我不希望 cookie 继续使用推荐 URL 重新创建,我试图只获取第一个引用的 URL。
$(document).ready(function(){
if ($.cookie('bas_referral') == null ){
var ref = document.referrer.toLowerCase();
// set cookie
var cookURL = $.cookie('bas_referral', ref, { expires: 1 });
}
});
Displaying the current cookie contents:
显示当前 cookie 内容:
// get cookie
alert($.cookie('bas_referral'));
// delete cookie
$.cookie('bas_referral', null);
回答by Laszlo
I think the bulletproof way is:
我认为防弹方法是:
if (typeof $.cookie('token') === 'undefined'){
//no cookie
} else {
//have cookie
}
Checking the type of a null, empty or undefined var always returns 'undefined'
检查 null、空或未定义 var 的类型总是返回 'undefined'
Edit:You can get there even easier:
编辑:您可以更轻松地到达那里:
if (!!$.cookie('token')) {
// have cookie
} else {
// no cookie
}
!!
will turn the falsyvaluesto false. Bear in mind that this will turn 0
to false!
!!
将会把falsy值设置为false。请记住,这将变为0
假!
回答by inertialmedia
$(document).ready(function() {
var CookieSet = $.cookie('cookietitle', 'yourvalue');
if (CookieSet == null) {
// Do Nothing
}
if (jQuery.cookie('cookietitle')) {
// Reactions
}
});
回答by Android334
I was having alot of trouble with this because I was using:
我在这方面遇到了很多麻烦,因为我正在使用:
if($.cookie('token') === null || $.cookie('token') === "")
{
//no cookie
}
else
{
//have cookie
}
The above was ALWAYS returning false, no matter what I did in terms of setting the cookie or not. From my tests it seems that the object is therefore undefined before it's set so adding the following to my code fixed it.
无论我是否在设置 cookie 方面做了什么,以上总是返回 false。从我的测试来看,该对象在设置之前似乎是未定义的,因此将以下内容添加到我的代码中即可修复它。
if($.cookie('token') === null || $.cookie('token') === ""
|| $.(cookie('token') === "null" || $.cookie('token') === undefined)
{
//no cookie
}
else
{
//have cookie
}
回答by ThomasAFink
You can set the cookie after having checked if it exists with a value.
您可以在检查它是否存在并带有值后设置 cookie。
$(document).ready(function(){
if ($.cookie('cookie')) { //if cookie isset
//do stuff here like hide a popup when cookie isset
//document.getElementById("hideElement").style.display = "none";
}else{
var CookieSet = $.cookie('cookie', 'value'); //set cookie
}
});
回答by Aldrin Masamoc Mojica
Try this very simple:
试试这个非常简单:
var cookieExist = $.cookie("status");
if(cookieExist == "null" ){
alert("Cookie Is Null");
}