标签:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookie</title> <script src="jquery.js" type="text/javascript"></script> </head> <body> <form id="formId" action=""> <input type="text" name="a" value="安安" /> <input type="hidden" name="b" value="abc"> </form> <script type="text/javascript"> /** * 设置cookie * @param {string} name 键名 * @param {string} value 键值 * @param {integer} days cookie周期 */ function setCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); }else{ var expires = ""; } document.cookie = name+"="+value+expires+"; path=/"; } // 获取cookie function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(‘;‘); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==‘ ‘) c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } // 删除cookie function deleteCookie(name) { setCookie(name,"",-1); } setCookie(‘userName‘,‘wh‘,0.001); // alert(getCookie(‘userName‘)); // deleteCookie(‘userName‘); </script> </body> </html>
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookies-2</title> <script src="jquery.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> /** * 设置cookie * @param {string} name 键名 * @param {string} value 键值 * @param {integer} days cookie周期 */ function setCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); }else{ var expires = ""; } document.cookie = name+"="+value+expires+"; path=/"; } // 获取cookie function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(‘;‘); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==‘ ‘) c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } // 删除cookie function deleteCookie(name) { setCookie(name,"",-1); } // setCookie(‘userName‘,‘wh‘,0.001); alert(getCookie(‘userName‘)); // deleteCookie(‘userName‘); </script> </body> </html>
标签:
原文地址:http://www.cnblogs.com/wuheng1991/p/5714505.html