通過Jquery.cookie.js實(shí)現(xiàn)展示瀏覽網(wǎng)頁的歷史記錄超管用
本文就是要利用cookie插件,獲取用戶瀏覽文章歷史記錄,并將用戶最近瀏覽歷史記錄顯示在頁面。
在需要添加cookie的頁面加上如下js
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript">
$(function(){
var art_title = $("title").html();
var art_url = document.URL;
var history;
var json="[";
//json1是第一次注入cookie以后的第一個(gè)json,"此時(shí)還不是數(shù)組" 以點(diǎn)帶面的處理
var json1;
var canAdd= true;
//var json1=eval("({sitename:'dreamdu',sitedate:new Date(1980, 12, 17, 12, 0, 0)})");
if(!$.cookie("history")){
//第一次的時(shí)候需要初始化
history = $.cookie("history","{title:\""+art_title+"\""+",url:\""+art_url+"\"}");
}else {
//已經(jīng)存在
history = $.cookie("history");
json1 = eval("("+history+")");
$(json1).each(function(){
if(this.title==art_title){
canAdd=false;
return false;
}
})
if(canAdd){
$(json1).each(function(){
json = json + "{\"title\":\""+this.title+"\",\"url\":\""+this.url+"\"},";
})
json = json + "{\"title\":\""+art_title+"\",\"url\":\""+art_url+"\"}]";
$.cookie("history",json,{expires:1});
}
}
})
</script>
在展示歷史記錄的頁面添加如下js
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript">
$(function(){
if($.cookie("history")){
var json = eval("("+$.cookie("history")+")");
var list ="";
$(json).each(function(){
list = list + "<li><a href='"+this.url+"' target='_blank'>"+this.title+"</a></li>";
alert(this.url);
})
$("#list").html(list);;
}
});
</script>
</head>
以上內(nèi)容是小編給大家分享的通過Jquery.cookie.js實(shí)現(xiàn)展示瀏覽網(wǎng)頁的歷史記錄,希望大家喜歡。
相關(guān)文章
jQuery實(shí)現(xiàn)嵌套選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)嵌套選項(xiàng)卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
簡單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)動態(tài))
這篇文章主要介紹了簡單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)),增強(qiáng)動態(tài)增加div效果,代碼簡單,很容易實(shí)現(xiàn),需要的朋友可以參考下2017-02-02
jquery+html5時(shí)鐘特效代碼分享(可設(shè)置鬧鐘并且語音提醒)
這篇文章主要為大家詳細(xì)介紹了Jquery+html5可設(shè)置鬧鐘并且會語音提醒的時(shí)鐘特效,功能實(shí)現(xiàn)非常簡單,推薦給大家,有需要的小伙伴可以參考下2015-08-08
jquery ui bootstrap 實(shí)現(xiàn)自定義風(fēng)格
本文主要是給大家分享了jQuery UI bootstrap的自定義風(fēng)格,以及自定義的方法,非常的實(shí)用,有需要的小伙伴千萬不要錯(cuò)過2014-11-11

