jsにおけるクッキーの追加、削除、照会


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>cookie      (    ,    :      )</title>
<script language="JavaScript" type="text/javascript">
function addCookie(objName,objValue,objHours){//  cookie
var str = objName + "=" + escape(objValue);
if(objHours > 0){// 0        ,      cookie    
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("  cookie  ");
}

function getCookie(objName){//       cookie  
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName) return unescape(temp[1]);
} 
}

function delCookie(name){//         cookie,                  
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}

function allCookie(){//       cookie   
var str = document.cookie;
if(str == ""){
str = "      cookie";
}
alert(str);
}

function $(m,n){
return document.forms[m].elements[n].value;
}

function add_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = $("myform","cookie_value");
var cookie_expireHours = $("myform","cookie_expiresHours");
addCookie(cookie_name,cookie_value,cookie_expireHours);
}

function get_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = getCookie(cookie_name);
alert(cookie_value);
}

function del_(){
var cookie_name = $("myform","cookie_name");
delCookie(cookie_name);
alert("    ");
}
</script>

</head>
<body>
<form name="myform">
<div><label for="cookie_name">  </label><input type="text" name="cookie_name" /></div>
<div><label for="cookie_value"> </lable><input type="text" name="cookie_value" /></div>
<div><label for="cookie_expireHours">       </lable><input type="text" name="cookie_expiresHours" /></div>
<div>
<input type="button" value="   cookie" onclick="add_()" />
<input type="button" value="    cookie" onclick="allCookie()" />
<input type="button" value="     cookie" onclick="get_()" />
<input type="button" value="     cookie" onclick="del_()" />
</div>
</form>
<hr />
</body>
</html>