一直以来都对cookie的操作比较的陌生.今天特意到网上找了资料看看学习学习.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js操作cookie</title>
<script type="text/javascript">
function getCookie(cookie_name)
{
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name);
// 如果找到了索引,就代表cookie存在,
// 反之,就说明不存在。
if (cookie_pos != -1)
{
// 把cookie_pos放在值的开始,只要给值加1即可。
cookie_pos += cookie_name.length + 1;
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -1)
{
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return value;
}
function setCookie(){
if(!navigator.cookieEnabled){
alert('不Cookie项!');
}else{
var date=new Date();
date.setTime(date.getTime()+60000*10);
document.cookie='userName='+escape(document.form1.userName.value)+';expires='+date.toGMTString()+';path=/'+';domaim=zgdsc.cn'+':secure';
}
}
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js操作cookie</title>
<script type="text/javascript">
function getCookie(cookie_name)
{
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name);
// 如果找到了索引,就代表cookie存在,
// 反之,就说明不存在。
if (cookie_pos != -1)
{
// 把cookie_pos放在值的开始,只要给值加1即可。
cookie_pos += cookie_name.length + 1;
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -1)
{
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return value;
}
function setCookie(){
if(!navigator.cookieEnabled){
alert('不Cookie项!');
}else{
var date=new Date();
date.setTime(date.getTime()+60000*10);
document.cookie='userName='+escape(document.form1.userName.value)+';expires='+date.toGMTString()+';path=/'+';domaim=zgdsc.cn'+':secure';
}
}
</script>
</head>
<body>
</body>
</html>