日期:2014-05-16  浏览次数:20398 次

看下
翻译下这代码的意思,详细点

<html>
<head>
<script type="text/javascript">
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 ""
}

function setCookie(c_name,value,expiredays)
{
var 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('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
  {
  setCookie('username',username,365)
  }
  }
}
</script>
</head>

<body onLoad="checkCookie()">
</body>
</html>


------解决方案--------------------
getCookie 获取 cookie
setCookie 设置 Cookie

function checkCookie()
{
username=getCookie('username') //获取 cookie
if (username!=null && username!="")
{alert('Welcome again '+username+'!')} // 获取到,弹出消息框
else 
{
username=prompt('Please enter your name:',"") //// 没获取到,让用户输入
if (username!=null && username!="")
{
setCookie('username',username,365) // 设置Cookie
}
}
}
------解决方案--------------------
function getCookie(c_name)
{
if (document.cookie.length>0)//有cookie的对象
{
c_start=document.cookie.indexOf(c_name + "=")//是否含有key为:c_name的cookie
if (c_start!=-1)//有查找到c_name的key
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)//截取止的位置如果cookie后面还有其它参数到;止
if (c_end==-1) c_end=document.cookie.length//如果没有;则截取到末尾
return unescape(document.cookie.substring(c_start,c_end))//取截取的值,然后解码返回
}
}
return ""//没有找到,返回空。
}