標籤:style blog http java os 檔案
/** * history_teacher.jsp中的js,最近瀏覽名師 * @version: 1.0 * @author: mingming */$(function(){getHistory();});var historyCount=4; //儲存記錄個數/** * 增加瀏覽歷程記錄 * @param id 編號 * @param name 姓名 * @param photo 照片 * @param grade 年級 * @param subject 科目 * @return */function setHistory(id,name,photo,grade,subject){stringCookie=getCookie(‘history‘); var stringHistory=""!=stringCookie?stringCookie:"{history:[]}"; var json=new JSONHistory(stringHistory); //轉成json var list = json[‘history‘]; //獲得json for (var i = 0; i < list.length; i++) { try { if(list[i].id == id){ list.splice(i,1); //重複資料刪除資料,開始位置,刪除個數 i=i-1; //下標歸位 }} catch (e) {break;}} if(list.length>=historyCount){ //刪除最開始的多餘記錄 var count = list.length - historyCount + 1; //需要刪除的個數 list.splice(0,count); //開始位置,刪除個數 } var e="{id:‘"+id+"‘,name:‘"+name+"‘,photo:‘"+photo+"‘,grade:‘"+grade+"‘,subject:‘"+subject+"‘}"; json[‘history‘].push(e);//添加一個新的記錄 setCookie(‘history‘,json.toString(),365); //365天}/** * 獲得瀏覽歷程記錄 * @return */function getHistory(){var historyJSON=getCookie(‘history‘);if(historyJSON==""){}else{var data = eval("("+historyJSON+")");var history = data[‘history‘]; //記錄var length = history.length;if(length > historyCount){length = historyCount;}//從最後一個瀏覽記錄開始擷取var historyHtml="";for ( var i = length-1; i >= 0; i--) {historyHtml+=‘<dd>‘+ ‘<ul>‘+ ‘<li class="recimg"><a href="‘+PATH+"/teacher/teacherinfo?teacherVO.teacherId="+history[i].id+‘"><img src="‘+PATH + "/"+ history[i].photo +‘" /></a></li>‘+ ‘<li class="recinfo">‘+ ‘<p><a href="‘+PATH+"/teacher/teacherinfo?teacherVO.teacherId="+history[i].id+‘">‘+history[i].name+‘</a></p>‘+ ‘<p class="grayp">科目:‘+history[i].subject+‘</p>‘+ ‘<p class="grayp">年級:‘+history[i].grade+‘</p>‘+ ‘</li>‘+ ‘</ul>‘+ ‘</dd>‘;}if(historyHtml!=""){$("#historyTeacher").html(‘<h2>最近瀏覽名師</h2><dl>‘+historyHtml+‘</dl>‘);}}}/** * 添加cookie * @param cookName cookie名稱 * @param cookName cookie值 * @param expiredays 時間長度 */function setCookie(cookName,cookValue,expiredays){ var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays*24*3600*1000); var cookieVal=cookName+ "=" +escape(cookValue)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"; document.cookie=cookieVal;}/** * 擷取cookie * @param cookName cookie名稱 * @return */function getCookie(cookName){ if(document.cookie.length>0){ var c_start = document.cookie.indexOf(cookName + "="); if(c_start!=-1){ //存在 c_start = c_start + cookName.length + 1; //"history="後的開始位置 var c_end=document.cookie.indexOf(";",c_start); //找到JSESSIONID在的位置 if (c_end==-1){ //JSESSIONID不存在 c_end=document.cookie.length; } return unescape(document.cookie.substring(c_start,c_end)); } } return "";}/** * JSON */var JSONHistory = function(sJSON){this.objType = (typeof sJSON);if(this.objType==‘string‘ && ‘‘==sJSON){ sJSON = ‘{history:[]}‘ ;} this.self = []; (function(s,o){ for(var i in o){ o.hasOwnProperty(i)&&(s[i]=o[i],s.self[i]=o[i]) };})(this,(this.objType==‘string‘)?eval(‘0,‘+sJSON):sJSON);};JSONHistory.prototype = { toString:function(){ return this.getString(); }, valueOf:function(){ return this.getString(); }, getString:function(){ var sA = []; (function(o){ var oo = null; sA.push(‘{‘); for(var i in o){ if(o.hasOwnProperty(i) && i!=‘prototype‘){ oo = o[i]; if(oo instanceof Array){ sA.push(i+‘:[‘); for(var b in oo){ if(oo.hasOwnProperty(b) && b!=‘prototype‘){ sA.push(oo[b]+‘,‘); if(typeof oo[b]==‘object‘) arguments.callee(oo[b]); } } sA.push(‘],‘); continue; }else{ sA.push(i+‘:\‘‘+oo+‘\‘,‘); } if(typeof oo==‘object‘) arguments.callee(oo); } } sA.push(‘},‘); })(this.self); return sA.slice(0).join(‘‘).replace(/\[object object\],/ig,‘‘).replace(/,\}/g,‘}‘).replace(/,\]/g,‘]‘).slice(0,-1); }, push:function(sName,sValue){ this.self[sName] = sValue; this[sName] = sValue; }};
中間對於把記錄轉換為json格式有更簡便的方法。這個是個單獨案例。
頁面引入js,增加瀏覽歷程記錄調用
setHistory(id,name,photo,grade,subject)
cookie參數設定:
path:
是相對於應用伺服器存放應用的檔案夾的根目錄而言的(比如tomcat下面的webapp),因此cookie.setPath("/");之後,可以在webapp檔案夾下的所有應用共用cookie
測試結果:不設定,則目前的目錄下所有頁面可以訪問,設定”/“後,所有頁面都可以訪問
domain:
在www.baidu.com中設定的cookie,但需要在zhidao.baidu.com下擷取,這樣就需要Cookie的setDomain()方法了。 cookie.setPath("/"); cookie.setDomain("baidu.com");//只給出網域名稱的相同部分
cookie參數設定參考文檔:
setPath和setCookie具體用法