[セットトップ]3.2 Javascript:クライアント-クッキーを探索する
7664 ワード
のホームページです
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cookie</title>
<!-- js -->
<script type="text/javascript" src="Cookie.js">// src html js </script>
</head>
<body">
<div onClick="setCookie()">
cookie
</div>
<div onClick="readCookike()">
cookie
</div>
<DIV></DIV>
<div onClick="clearCookie()">
cookie
</div>
</body>
</html>
jsコード// JavaScript Document
//javascriipt + cookie = 、
//cookie
// cookie
//cookie , , ,
//
//cookie ,
// ,cookie ,
//cookie 4kb ,
// cookie
//
function setCookie() {
//navigator
// cookie
if(!navigator.cookieEnabled)
{
alert("cookieEnabled is false");
return;
}
// cookie cookie
var cookieID="uName";
var cookieValue="shiqiqi";
var date=5*365;
// writeCookie
writeCookie(cookieID,cookieValue,date);
alert("write the cookie");
}
function writeCookie(name,value,days) {
var date=new Date();//
date.setTime(date.getTime()+(days*24*60*60*1000));//setTime() Date 。getTime() 1970 1 1 。
var expires="; expires=" +date.toGMTString();//expires
document.cookie=name+"="+value+expires+";path=/";// cookie
}
function readCookike() {
var name="uName";//cookie
var seachName=name+"=";
var cookies=document.cookie.split(";");// Cookies
if(!cookies)//if , false, true
{
alert("no cookies");
return;
}
for(var i=0;i<cookies.length;i++)// Cookies
{
var c=cookies[i];// Cookies
while(c.charAt(0)==" ")//charAt(index)
{
//
c=c.substring(1,c.length);//substring(start,stop) 。
}
if(c.indexOf(seachName)==0)//indexOf()
{
// cookie seachName
// cookie
c=c.substring(seachName.length,c.length);
alert(c);
}
else{
alert(null);
}
}
}
function clearCookie() {
var name="uName";
writeCookie(name,"",-1);
// cookie, "", -1
alert("clear the cookie");
}