// -------------------------
// JavaScript的Cookies函式库
// -------------------------
// 保存Cookie
function saveCookie(name, value, expires, path, domain, secure){
var strCookie = name + "=" + value;
if (expires){
// 计算Cookie的期限, 参数为天数
var curTime = new Date();
curTime.setTime(curTime.getTime() + expires*24*60*60*1000);
strCookie += "; expires=" + curTime.toGMTString();
}
// Cookie的路径
strCookie += (path) ? "; path=" + path : "";
// Cookie的Domain
strCookie += (domain) ? "; domain=" + domain : "";
// 是否需要保密传送,为一个布尔值
strCookie += (secure) ? "; secure" : "";
document.cookie = strCookie;
}
// 使用名称参数获取Cookie值, null表示Cookie不存在
function getCookie(name){
var strCookies = document.cookie;
var cookieName = name + "="; // Cookie名称
var valueBegin, valueEnd, value;
// 寻找是否有此Cookie名称
valueBegin = strCookies.indexOf(cookieName);
if (valueBegin == -1) return null; // 没有此Cookie
// 获取值的结尾位置
valueEnd = strCookies.indexOf(";", valueBegin);
if (valueEnd == -1)
valueEnd = strCookies.length; // 最后一个Cookie
// 获取Cookie值
value = strCookies.substring(valueBegin+cookieName.length,valueEnd);
return value;
}
// 检查Cookie是否存在
function checkCookieExist(name){
if (getCookie(name))
return true;
else
return false;
}
// 删除Cookie
function deleteCookie(name, path, domain){
var strCookie;
// 检查Cookie是否存在
if (checkCookieExist(name)){
// 设定Cookie的期限为己过期
strCookie = name + "=";
strCookie += (path) ? "; path=" + path : "";
strCookie += (domain) ? "; domain=" + domain : "";
strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
document.cookie = strCookie;
}
}
我想问的是在checkCookieExist()中,if(getCookie(name)) return true;这种写法是什么意思,他用到了getCookie()方法,
返回值是value,那就是if(value),没有条件表达式啊,能这样写吗,这种写是什么意思。
// JavaScript的Cookies函式库
// -------------------------
// 保存Cookie
function saveCookie(name, value, expires, path, domain, secure){
var strCookie = name + "=" + value;
if (expires){
// 计算Cookie的期限, 参数为天数
var curTime = new Date();
curTime.setTime(curTime.getTime() + expires*24*60*60*1000);
strCookie += "; expires=" + curTime.toGMTString();
}
// Cookie的路径
strCookie += (path) ? "; path=" + path : "";
// Cookie的Domain
strCookie += (domain) ? "; domain=" + domain : "";
// 是否需要保密传送,为一个布尔值
strCookie += (secure) ? "; secure" : "";
document.cookie = strCookie;
}
// 使用名称参数获取Cookie值, null表示Cookie不存在
function getCookie(name){
var strCookies = document.cookie;
var cookieName = name + "="; // Cookie名称
var valueBegin, valueEnd, value;
// 寻找是否有此Cookie名称
valueBegin = strCookies.indexOf(cookieName);
if (valueBegin == -1) return null; // 没有此Cookie
// 获取值的结尾位置
valueEnd = strCookies.indexOf(";", valueBegin);
if (valueEnd == -1)
valueEnd = strCookies.length; // 最后一个Cookie
// 获取Cookie值
value = strCookies.substring(valueBegin+cookieName.length,valueEnd);
return value;
}
// 检查Cookie是否存在
function checkCookieExist(name){
if (getCookie(name))
return true;
else
return false;
}
// 删除Cookie
function deleteCookie(name, path, domain){
var strCookie;
// 检查Cookie是否存在
if (checkCookieExist(name)){
// 设定Cookie的期限为己过期
strCookie = name + "=";
strCookie += (path) ? "; path=" + path : "";
strCookie += (domain) ? "; domain=" + domain : "";
strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
document.cookie = strCookie;
}
}
我想问的是在checkCookieExist()中,if(getCookie(name)) return true;这种写法是什么意思,他用到了getCookie()方法,
返回值是value,那就是if(value),没有条件表达式啊,能这样写吗,这种写是什么意思。
其实条件表达式是有值的呀,一般是true,false!而上述你说的if(value)就相当于if(true)或if(false)只是将表达式返回值放进去了,与表达式是一个效果...楼主是不是对基本类型,变量还理解的不够清楚呀?一起学习...