日期:2014-05-16 浏览次数:20374 次
function getCookie(c_name) //检查是否已设置cookie
{
if(document.cookie.length>0)
{
c_start = document.cookie.indexOf(c_name+"=") //indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置。
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))//unescape 方法返回一个包含 charstring 内容的字符串值
}
}
return ""
}
function setCookie(c_name,value,expiredays) //创建一个cookie
{
exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+"="+escape(value)+((expiredays==null)?";":";expires="+exdate.toGMTString())
}
function checkCookie()
{
username=getCookie('username')
if(username != null && username != "")
{
alert('欢迎再次回来'+username+'!')
}
else
{
username=prompt('请输入你的名字:',"")
if(username != null && username != "")
{
setCookie('username',username,365)
}
}
}
checkCookie()