一直都是簡單去js實現cookie的一些操做,今天把js對cookie操做系統的整理了一遍,包括:js讀取cookie,js添加cookie,js刪除cookie,示例以下:javascript
<!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> <mce: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(); } //讀取出來全部的cookie字筗串了 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("刪除成功"); } // --></mce:script> //添加cookie function addCookie(name,value,expires,path,domain){ var str=name+"="+escape(value); if(expires!=""){ var date=new Date(); date.setTime(date.getTime()+expires*24*3600*1000);//expires單位爲天 str+=";expires="+date.toGMTString(); } if(path!=""){ str+=";path="+path;//指定可訪問cookie的目錄 } if(domain!=""){ str+=";domain="+domain;//指定可訪問cookie的域 } document.cookie=str; } //取得cookie function getCookie(name){ var str=document.cookie.split(";") for(var i=0;i<str.length;i++){ var str2=str[i].split(」=」); if(str2[0]==name)return unescape(str2[1]); } } //刪除cookie function delCookie(name){ var date=new Date(); date.setTime(date.getTime()-10000); document.cookie=name+」=n;expire=」+date.toGMTString(); } //刪除全部cookie function delAllCookie() { tmpArr = document.cookie.split(";"); delKey = new Array(); for(i in tmpArr) { if(tmpArr[i].indexOf("mykeywords") != -1) { delKey.push(decodeURIComponent(tmpArr[i].split("=")[0])); } } today = new Date(); today.setTime(today.getTime() - 10000); for(i in delKey) { name = delKey[i]; document.cookie = name+"='';expires="+today.toGMTString(); } tmpArr = delKey = today = ""; }
// Date() 的構造器設置以毫秒爲單位 // .getTime() 方法返回時間,單位爲毫秒 // 因此要設置15分鐘到期,要用60000毫秒乘15分鐘 var expiration = new Date((new Date()).getTime() + 15 * 60000); document.cookie = "username=" + escape(form.username.value)+ "; expires =" + expiration.toGMTString() + "; path=" + "/" + "; _ domain=" + "mydomain.com" + "; secure"; // 咱們定義一個函數,用來讀取特定的cookie值。[獲得指定名字的cookie對象哦!] function getCookie(cookie_name) { var allcookies = document.cookie; var cookie_pos = allcookies.indexOf(cookie_name); // 若是找到了索引,就表明cookie存在, // 反之,就說明不存在。 if (cookie_pos != -1) { // 把cookie_pos放在值的開始,只要給值加1便可。 cookie_pos += cookie_name.length + 1; var cookie_end = allcookies.indexOf(";", cookie_pos); if (cookie_end == -1) { cookie_end = allcookies.length; } var value = unescape(allcookies.substring(cookie_pos, cookie_end)); } return value; } // 調用函數 var cookie_val = getCookie("username");
<%@ page contentType="text/html; charset=ISO8859_1" %> <% Cookie _cookie=new Cookie("user_delfancom", "delfan"); _cookie.setMaxAge(30*60); // 設置Cookie的存活時間爲30分鐘 response.addCookie(_cookie); // 寫入客戶端硬盤 out.print("寫Cookie完成"); %>
<% Cookie cookies[]=request.getCookies(); // 將適用目錄下全部Cookie讀入並存入cookies數組中 Cookie sCookie=null; String sname=null; String name=null; if(cookies==null) // 若是沒有任何cookie out.print("none any cookie"); else { out.print(cookies.length + "<br>"); for(int i=0;i<cookies.length; i++) // 循環列出全部可用的Cookie { sCookie=cookies[i]; sname=sCookie.getName(); name = sCookie.getValue(); out.println(sname + "->" + name + "<br>"); } } %>須要注意的兩個問題: 1. Cookie有個適用路徑的問題, 就是說若是 writecookie.jsp和readcookie.jsp要放在贊成目錄下, 若是不在同一目錄下, 則寫的時候須要設置路徑,爲readcookie.jsp所在的路徑. 2. 讀入Cookie數組的時候須要判斷是否爲空(null), 網上不少代碼都沒有寫出這一點.