你知道我们可以通过java程序来操作cookie,非常的方便,如果你想更方便的话,你可以通过JS在页面级别操作cookie。见:Java操作cookie
你可以直接参考一下这两篇文章:可以多了解点知识:参考1,参考2
/** * 设置cookie * c_name:cookie的key值 * c_value:cookie的value值 * expiredays:过期时间,单位为天 */ function setCookie(c_name,c_value,expiredays) { //1.设置cookie过期时间 var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); //2.设置cookie //document.cookie = c_name + "=" + escape(c_value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //document.cookie = c_name + "=" + escape(c_value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/"; //加路径的 //document.cookie = c_name + "=" + escape(c_value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;domain=.kongzhong.com"; //加路径的和域的 document.cookie = c_name + "=" + escape(c_value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;domain=.kongzhong.com;secure"; //加路径的和域,安全的 /** * 如果是本机测试,你要测试含域的情况,可以这样 *你需要去C:\Windows\System32\drivers\etc\hosts中配置:127.0.0.1 local.kongzhong.com 同时访问时路径:http://local.kongzhong.com:8080/cookie/cookie1.htm */ } /** * 获取cookie * c_name:cookie的key值 */ function getCookie(c_name) { if(document.cookie.length > 0) { //如果cookie为空 var c_start = document.cookie.indexOf(c_name + "="); //检查key为c_name的cookie是否存在 if(c_start != -1) { c_start = c_start + c_name.length + 1; //获取cookie值开始的位置 var c_end = document.cookie.indexOf(";",c_start); //获取cookie值结束的位置;因为需要考虑是否是最后一项,所以通过";"号是否存在来判断 if(c_end == -1) { c_end = document.cookie.length; } return unescape(document.cookie.substring(c_start, c_end)); } } return ""; } /** * 删除cookie * @param c_name cookie的key值 */ function delCookie(c_name) { //setCookie(c_name,null,-30); 只要把第三个参数设置成负值就行了 setCookie(c_name,"",-30); }
下面给您一个下载名称:您可以下载demo看看包括java操作cookie和JS操作cookie。以及登陆的cookie操作。cookie_han_new
======================20130831-add=========
navigator.cookieEnabled 可以判断浏览器是否禁用cookie.
window.onload = function() { setCookie("sum",2,new Date(2013,11,10,19,30,30)); if(navigator.cookieEnabled) { var sum = readCookie("sum"); if(sum) { var isSum = parseInt(sum) + 1; alert("cookie count is" + isSum); if(isSum > 5) { eraseCookie("sum",new Date(2000,11,10,19,30,30)); } else { setCookie("sum",isSum,new Date(2013,11,10,19,30,30)); console.log(readCookie("sum")); } } else { alert("no cookie,setting now"); } } }
/** * [setCookie description] 设置cookie * @param {[type]} key [description] cookie的键 * @param {[type]} value [description] cookie的值 * @param {[type]} expireDate [description] cookie的有效期 * * 说明:使用encodeURI函数可以将cookie值中的特殊元素做转义处理。这样可以使你的cookie更加安全,因为它可以 * 抹掉实际的HTML代码,包括脚本元素。另外,encodeURI可以使cookie格式中使用到的特殊字符用于cookie值中,包括“=”和“;”; */ function setCookie(key,value,expireDate) { //var cookieDate = new Date(2013,11,10,19,30,30); document.cookie = key + "=" + encodeURI(value) + "; expires=" + expireDate.toGMTString() + "; path=/"; } /** * [readCookie description] 读取cookie * @param {[type]} key [description] * @return {[type]} [description] */ function readCookie(key) { var cookie = document.cookie; console.log(cookie); var first = cookie.indexOf(key + "="); if(first >= 0) { //存在cookie var str = cookie.substring(first,cookie.length); var last = str.indexOf(";"); if(last < 0) { //表示是最后一个cookie last = str.length; } //获取cookie的值 str = str.substring(0,last).split("="); return decodeURI(str[1]); } else { //不存在cookie return null; } } /** * [eraseCookie description] 将cookie的有效期设置成过去,以达到删除cookie的目的 * @param {[type]} key [description] * @return {[type]} [description] */ function eraseCookie(key,eraseDate) { // var cookieDate = new Date(2000,11,10,19,30,30); document.cookie = key + "= ;expires=" + eraseDate.toGMTString() + "; path=/"; }
2014-07-07-add-简化版
<script type="text/javascript"> var kzCookie = { g: function(b) { var a = document.cookie.match(new RegExp("(^| )" + b + "=([^;]*)(;|$)")); if (a != null) { return unescape(a[2]) } return null }, d: function(a) { var c = new Date(); c.setTime(c.getTime() - 1); var b = this.g(a); if (b != null) { document.cookie = a + "=" + b + ";expires=" + c.toGMTString() + ";path=/" } }, s: function(a, c, b) { var e = 30; var d = new Date(); if (b) { d.setTime(d.getTime() + b * 60 * 60 * 1000) } else { d.setTime(d.getTime() + e * 24 * 60 * 60 * 1000) } document.cookie = a + "=" + escape(c) + ";expires=" + d.toGMTString() + ";path=/" } }; kzCookie.s("hanchao","testcookienew",24); kzCookie.s("hanchao1","testcookienew1",24); kzCookie.d("hanchao"); //kzCookie.d("hanchao1"); var cookieValue = kzCookie.g("hanchao"); var cookieValue1 = kzCookie.g("hanchao1"); alert(cookieValue); alert(cookieValue1); </script>