?????? ??
??? ??????
??? ??? ???? ??? ??? ???? ??? ?????.
? ??? ? ???? ????? ?? ? ??? ??? ??? ??? ??? ??? ???? ????.
??? "????? ??? ??? ??? ???? ??"? ???? ? ?????.
???? ? ???? ???? ?? ???? ??? ??? ??? ? ????.
???? ???? ? ???? ??? ? ??? ?? ??? ??? ??? ?? ? ????.
??? ??? ?? ??/? ??? ?????.
username=John Doe
????? ???? ????? ???? ?? ???? ?? ??? ??? ?????. ??? ??? ???? ??? ??? ????.
?? ?? ?? ??
?? ??? ?? ??? ??? ?? ??? ????? ??? ? ????. ??? ??? ????.
document.cookie [ = name1=value1; ??=GMT_String; path=; domain=;]
?? ? ??
?? ? ?? ??? ???? ???.
user_id=2 ;user_name=admin;
JavaScript ??? ??? ????.
document.cookie = "user_name=admin";
?? ?? ?? ?? , ?? ? ??
?? ???? ?? ??? 1?? ???? ??? ? ??? ?? ?????? ? ??? ?? ??? ?? ??? ?????.
//?? ?? ????
var date=new ?? ();
date.setTime(date.getTime ()+24*3600*1000); document.cookie = "user_id=2;path=/;domain=.5idev.com;expire="+date.toGMTString( );
??? ?? ???? ?? ????(;), ??(,), ??(=) ? ??? ??? ? ????. ?? ????? ???? ?? escape() ??? ???? ???. ?? ?? ??? 16??? ??? ? ????.
document.cookie = "test="+escape("JavaScript cookie test");
?? ?? ? unescape()? ???? ????? ?? ?? ?? ????. ? ??? ???? ??? ??? ????? ??? ? ????. ??. ????.
?? ? ????
document.cookie? ?? ?? ???? ?? ?? ?? ??? ? ??? ??? ??????. ?? ???? ?? ???? ?? ??(?? ??)? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>與本文檔相關(guān)的 cookies(如果存在的話):</p> <script type="text/javascript"> document.write(document.cookie) </script> </body> </html>
?? ?? ???? ??
????? ??? ?????? ???? ??? ?? ? ????.
??? ???? ??? ??? ???? ?????.
??? ???? ?? ?? ???? ???? ???? ??? ??, setCookie ??? ???? ???? ??? 365? ?? ?????.
function checkCookie() { var username=getCookie("username"); if (username!="") { alert("Welcome again " + username); } else { username = prompt("Please enter your name:",""); if (username!="" && username!=null) { setCookie("username",username,365); } } }
JavaScript? ???? ?? ?? cookie
JavaScript? ??? .cookie ??? ???? ??? ???? ?? ??? ? ????.
JavaScript?? ??? ?? ??? ????.
document.cookie="username=John Doe";
??? ?? ??(UTC ?? GMT ??)? ??? ?? ????. ????? ??? ????? ??? ?????.
document.cookie="username=John Doe; ??=Thu, 18 Dec 2013 12:00:00 GMT";
??? ???? ????? ?? ? ????. path ???? ?? ??? ?????. ????? ??? ?? ???? ????.
document.cookie="username=John Doe; ??=2013? 12? 18? ??? 12:00:00 GMT; path=";
JavaScript? ???? ?? ??
JavaScript?? ??? ??? ? ????. ??? ???? ?? ??? ??????.
var x = document.cookie;
?: document.cookie? ??? ???? ?? ??? ?????. ??: cookie1=value; cookie2=value cookie3; =?;
JavaScript? ???? ?? ??
JavaScript?? ?? ??? ??? ?? ?? ??? ?????.
document.cookie="username=John Smith; ??=Thu, 18 Dec 2013 12:00:00 GMT; path=";
??? ??? ?????.
JavaScript? ???? ?? ??
??? ???? ?? ?? ????. ??? ?? ?? ????? ?? ???? ????? ?? ???. Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; ??=Thu, 01 Jan 1970 00 :00:00 GMT";
??? ? ?? ?? ???? ??? ??? ?? ?????.
?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <head> <script> function setCookie(cname,cvalue,exdays){ var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var expires = "expires="+d.toGMTString(); document.cookie = cname+"="+cvalue+"; "+expires; } function getCookie(cname){ var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++) { var c = ca[i].trim(); if (c.indexOf(name)==0) return c.substring(name.length,c.length); } return ""; } function checkCookie(){ var user=getCookie("username"); if (user!=""){ alert("Welcome again " + user); } else { user = prompt("Please enter your name:",""); if (user!="" && user!=null){ setCookie("username",user,30); } } } </script> </head> <body onload="checkCookie()"></body> </html>