Example of viewing history using js

Source: Internet
Author: User

You must first reference a js jquery. cookie. js

1. Write the jsCookied
Copy codeThe Code is as follows:
// The browsing record is written to 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 :"/"});
}
// The browsing record is written to JSCookied and ends.

2, and then read Cookied
Copy codeThe Code is as follows:
<Script type = "text/javascript" charset = "UTF-8">
// Read the cookied history
$ (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] + '"> </a> <p> ¥' + splithtml [I]. split ("|") [3] + '</p> </div> ');
Xhtml. push ('<div class = "otherInfo clearfix"> <span> sold <B>' + splithtml [I]. split ("|") [4] + '</B> pen </span> <a href = "' + splithtml [I]. split ("|") [2] + '"> let's see </a> </div> ');
}
Xhtml. push ('<div class = "tr"> <a href = "javascript: clearcookied ();"> clear records </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] + '"> </a> <p> ¥' + hc. split ("|") [3] + '</p> </div> ');
Xhtml. push ('<div class = "otherInfo clearfix"> <span> sold <B>' + hc. split ("|") [4] + '</B> </span> <a href = "' + hc. split ("|") [2] + '"> let's see </a> </div> ');
Xhtml. push ('<div class = "tr"> <a href = "javascript: clearcookied ();"> clear records </a> </div> ');
$ ("# MyHistory"). append (xhtml. join (""));
}
}
Else
{
$ ("# MyHistory"). append ("<li> no browsing history </li> ");
}
});

Function clearcookied ()
{
$. Cookie ('History ', null, {expires: 1, domain:' .nrqiang.com '});
$ ("# MyHistory" ).html ("<li> no browsing history </li> ");
}
$ (Function (){
$ ("Img"). each (function () {$ (this). attr ("src", $ (this). attr ("original "));});
});
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.