创建
share_position_cookie.txt $(function() {
//初始化获取名为hisArt的cookie (里面存放的是json对象)
var json = eval("("+$.cookie("hisArt")+")");
var list = "";
//如果有缓存,则取出遍历
if(json != null){
//css
$("#cookieList").css({display: ""});
$("#clearBtn").css({display: ""});
//document.getElementById("demo").style.display = "";
var boarddiv ="";
for(var i=0; i
//list = list + ""+json[i].title+"";
boarddiv = boarddiv + ""+json[i].location+"
"+json[i].salary+"
"+json[i].reward+"
";
}
//构建页面布局
$("#list").html(boarddiv);
}else{
//如果不存在则清除记录历史的位置
$("#cookieList").css({display: "none"});
$("#clearBtn").css({display: "none"});
}
//创建
createHistory();
});
——————————————————————————--------
function createHistory(){
//获取页面元素
var art_title = $("#positionName").val(); //文章标题
art_title = art_title.replace("'","");
var art_location = $("#location").val();
art_location = art_location.replace("\'","");
var art_salary = $("#salary").val();
art_salary = art_salary.replace("\'","");
var art_reward = $("#reward").val();
art_reward = art_reward.replace("\'","");
var art_url = document.URL; //页面地址
var canAdd = true; //初始可以插入cookie信息
var hisArt = $.cookie("hisArt");
var len = 0;
//初始化长度
if(hisArt){
hisArt = eval("("+hisArt+")");
len = hisArt.length;
}
//是否是重复访问
var hisArt_json = eval("("+$.cookie("hisArt")+")");
var list = "";
if(hisArt_json != null){
//如果cookie中存在该页面访问过的记录,则不放入cookie
for(var i=0; i if(art_title == hisArt_json[i].title){
canAdd == false;
return false;
}
}
}
//添加
if(canAdd == true){
var json = "[";
var start = 0;
var endJson = "]";
if(len>5){start = 1;}
if(len == 3){
//5
//art_title art_url
var title = art_title;
var url = art_url;
var salary = art_salary;
var location = art_location;
var reward = art_reward;
//0 1 2 3 4 只记录最近访问的4个历史
for(var i=start;i<3;i++){
if(i == len -1){
json = json + "{\"title\":\""+title+"\",\"url\":\""+url+"\",\"location\":\""+location+"\",\"salary\":\""+salary+"\",\"reward\":\""+reward+"\"}";
}
else{
json = json + "{\"title\":\""+title+"\",\"url\":\""+url+"\",\"location\":\""+location+"\",\"salary\":\""+salary+"\",\"reward\":\""+reward+"\"},";
}
title = hisArt[i].title;
url = hisArt[i].url;
salary = hisArt[i].location;
location =hisArt[i].salary;
reward = hisArt[i].reward;
}
// alert(json + "_____________________p2");
}else{
//。如果多了重新替换
if(len > 0){
json = json + "{\"title\":\""+art_title+"\",\"url\":\""+art_url+"\",\"location\":\""+art_location+"\",\"salary\":\""+art_salary+"\",\"reward\":\""+art_reward+"\"},";
}
else{
json = json + "{\"title\":\""+art_title+"\",\"url\":\""+art_url+"\",\"location\":\""+art_location+"\",\"salary\":\""+art_salary+"\",\"reward\":\""+art_reward+"\"}";
}
for(var i=start;i if(i == len -1){
json = json + "{\"title\":\""+hisArt[i].title+"\",\"url\":\""+hisArt[i].url+"\",\"location\":\""+hisArt[i].location+"\",\"salary\":\""+hisArt[i].salary+"\",\"reward\":\""+hisArt[i].reward+"\"}";
}
else{
json = json + "{\"title\":\""+hisArt[i].title+"\",\"url\":\""+hisArt[i].url+"\",\"location\":\""+hisArt[i].location+"\",\"salary\":\""+hisArt[i].salary+"\",\"reward\":\""+hisArt[i].reward+"\"},";
}
}
//alert(json + "_____________________p2");
//alert(json + "_____________________p3");
}
json = json + endJson;
// alert(json);
/**
*
* 如果你有其他页面需要共享该cookie,则必须设置该cookie的作用域,否则获取不到。
*/
$.cookie("hisArt",json,{expires:30, path: '/'});
}
}
------------------------------------
function clearHistory(){
if($.cookie("hisArt") == null){
alert("您浏览职位的历史记录已清空");
}else{
$.cookie("hisArt",null, { path: '/' });
$("#clearBtn").css({display: "none"});
$("#list").html("您已清空最近浏览过的职位
");
}
}
阅读(6463) | 评论(1) | 转发(2) |