js 實現瀏覽歷程記錄樣本

來源:互聯網
上載者:User

先必須要引用一個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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.