js 實現(xiàn)瀏覽歷史記錄示例
更新時間:2014年04月20日 14:35:15 作者:
這篇文章主要介紹了如何使用js實現(xiàn)瀏覽歷史記錄,需要的朋友可以參考下
先必須要引用一個js jquery.cookie.js
1,首先寫入jsCookied
//瀏覽記錄 寫入JSCookied 開始
var img=$("#ProductImgurl").attr("jqimg");
var name=$("#ProductDetail_ctl00_LabelName").text();
var url=location.href;
var price=$("#ProductDetail_ctl00_LabelShopPrice").text();
var sellcount=$("#ProductDetail_ctl00_lblSaleNumber").text();
var hc=img+"|"+name+"|"+url+"|"+price+"|"+sellcount;
if($.cookie("history")!=null)
{
if($.cookie("history").indexOf(name)==-1)
{
$.cookie("history",hc+"*"+$.cookie("history"),{expires:8,domain:'.groupfly.com',path:"/"});
}
}
else
{
$.cookie("history",hc,{expires:8,domain:'.groupfly.com',path:"/"});
}
//瀏覽記錄 寫入JSCookied 結束
2,然后再讀取Cookied
<script type="text/javascript" charset="utf-8">
//讀取cookied歷史記錄
$(function(){
var hc=$.cookie("history");
if(hc!=null)
{
if(hc.indexOf("*")!=-1)
{
var splithtml=hc.split("*");
var xhtml=new Array();
var hlength=splithtml.length;
if(parseInt(hlength)>4)
hlength=4;
for(var i=0;i<parseInt(hlength);i++)
{
xhtml.push('<div class="tuijian"><div class="tjname"><a href="'+splithtml[i].split("|")[2]+'">'+splithtml[i].split("|")[1].substr(0, 17)+'</a></div>');
xhtml.push('<div class="tjimga"><a href="'+splithtml[i].split("|")[2]+'"><img src="'+splithtml[i].split("|")[0]+'_160x160.jpg" alt="'+splithtml[i].split("|")[1]+'" /></a><p>¥'+splithtml[i].split("|")[3]+'</p></div>');
xhtml.push('<div class="otherInfo clearfix"><span>已銷售<b>'+splithtml[i].split("|")[4]+'</b>筆</span><a href="'+splithtml[i].split("|")[2]+'">再看看</a></div></div>');
}
xhtml.push('<div class="tr"><a href="javascript:clearcookied();">清空記錄</a></div>');
$("#MyHistory").append(xhtml.join(""));
}
else{
var xhtml=new Array();
xhtml.push('<div class="tuijian"><div class="tjname"><a href="'+hc.split("|")[2]+'">'+hc.split("|")[1].substr(0, 17)+'</a></div>');
xhtml.push('<div class="tjimga"><a href="'+hc.split("|")[2]+'"><img src="'+hc.split("|")[0]+'_160x160.jpg" alt="'+hc.split("|")[1]+'" /></a><p>¥'+hc.split("|")[3]+'</p></div>');
xhtml.push('<div class="otherInfo clearfix"><span>已銷售<b>'+hc.split("|")[4]+'</b>筆</span><a href="'+hc.split("|")[2]+'">再看看</a></div></div>');
xhtml.push('<div class="tr"><a href="javascript:clearcookied();">清空記錄</a></div>');
$("#MyHistory").append(xhtml.join(""));
}
}
else
{
$("#MyHistory").append("<li>暫無瀏覽記錄</li>");
}
});
function clearcookied()
{
$.cookie('history',null,{expires:1,domain:'.nrqiang.com'});
$("#MyHistory").html("<li>暫無瀏覽記錄</li>");
}
$(function() {
$("img").each(function(){$(this).attr("src",$(this).attr("original"));});
});
</script>
1,首先寫入jsCookied
復制代碼 代碼如下:
//瀏覽記錄 寫入JSCookied 開始
var img=$("#ProductImgurl").attr("jqimg");
var name=$("#ProductDetail_ctl00_LabelName").text();
var url=location.href;
var price=$("#ProductDetail_ctl00_LabelShopPrice").text();
var sellcount=$("#ProductDetail_ctl00_lblSaleNumber").text();
var hc=img+"|"+name+"|"+url+"|"+price+"|"+sellcount;
if($.cookie("history")!=null)
{
if($.cookie("history").indexOf(name)==-1)
{
$.cookie("history",hc+"*"+$.cookie("history"),{expires:8,domain:'.groupfly.com',path:"/"});
}
}
else
{
$.cookie("history",hc,{expires:8,domain:'.groupfly.com',path:"/"});
}
//瀏覽記錄 寫入JSCookied 結束
2,然后再讀取Cookied
復制代碼 代碼如下:
<script type="text/javascript" charset="utf-8">
//讀取cookied歷史記錄
$(function(){
var hc=$.cookie("history");
if(hc!=null)
{
if(hc.indexOf("*")!=-1)
{
var splithtml=hc.split("*");
var xhtml=new Array();
var hlength=splithtml.length;
if(parseInt(hlength)>4)
hlength=4;
for(var i=0;i<parseInt(hlength);i++)
{
xhtml.push('<div class="tuijian"><div class="tjname"><a href="'+splithtml[i].split("|")[2]+'">'+splithtml[i].split("|")[1].substr(0, 17)+'</a></div>');
xhtml.push('<div class="tjimga"><a href="'+splithtml[i].split("|")[2]+'"><img src="'+splithtml[i].split("|")[0]+'_160x160.jpg" alt="'+splithtml[i].split("|")[1]+'" /></a><p>¥'+splithtml[i].split("|")[3]+'</p></div>');
xhtml.push('<div class="otherInfo clearfix"><span>已銷售<b>'+splithtml[i].split("|")[4]+'</b>筆</span><a href="'+splithtml[i].split("|")[2]+'">再看看</a></div></div>');
}
xhtml.push('<div class="tr"><a href="javascript:clearcookied();">清空記錄</a></div>');
$("#MyHistory").append(xhtml.join(""));
}
else{
var xhtml=new Array();
xhtml.push('<div class="tuijian"><div class="tjname"><a href="'+hc.split("|")[2]+'">'+hc.split("|")[1].substr(0, 17)+'</a></div>');
xhtml.push('<div class="tjimga"><a href="'+hc.split("|")[2]+'"><img src="'+hc.split("|")[0]+'_160x160.jpg" alt="'+hc.split("|")[1]+'" /></a><p>¥'+hc.split("|")[3]+'</p></div>');
xhtml.push('<div class="otherInfo clearfix"><span>已銷售<b>'+hc.split("|")[4]+'</b>筆</span><a href="'+hc.split("|")[2]+'">再看看</a></div></div>');
xhtml.push('<div class="tr"><a href="javascript:clearcookied();">清空記錄</a></div>');
$("#MyHistory").append(xhtml.join(""));
}
}
else
{
$("#MyHistory").append("<li>暫無瀏覽記錄</li>");
}
});
function clearcookied()
{
$.cookie('history',null,{expires:1,domain:'.nrqiang.com'});
$("#MyHistory").html("<li>暫無瀏覽記錄</li>");
}
$(function() {
$("img").each(function(){$(this).attr("src",$(this).attr("original"));});
});
</script>
您可能感興趣的文章:
- 使用JS location實現(xiàn)搜索框歷史記錄功能
- JS返回頁面時自動回滾到歷史瀏覽位置
- jquery使用Cookie和JSON記錄用戶最近瀏覽歷史
- 通過Jquery.cookie.js實現(xiàn)展示瀏覽網(wǎng)頁的歷史記錄超管用
- javascript history對象(歷史記錄)使用方法(實現(xiàn)瀏覽器前進后退)
- javascript 歷史記錄 經(jīng)常用于產(chǎn)品最近歷史瀏覽
- JavaScript入門教程(7) History歷史對象
- javascript結合Cookies實現(xiàn)瀏覽記錄歷史
- JavaScript中的History歷史對象
- javascript-hashchange事件和歷史狀態(tài)管理實例分析
相關文章
JavaScript代碼異常監(jiān)控實現(xiàn)過程詳解
這篇文章主要介紹了JavaScript代碼異常監(jiān)控實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02Uniapp中嵌入H5并在H5中跳轉到APP的指定頁面方法詳解
Uniapp是一款基于Vue.js框架的跨平臺開發(fā)工具,支持在一套代碼中開發(fā)出運行于各大平臺的應用程序,這篇文章主要給大家介紹了關于Uniapp中嵌入H5并在H5中跳轉到APP的指定頁面的相關資料,需要的朋友可以參考下2023-09-09