Use the cookie in JS to realize the browsing record of the product.

Source: Internet
Author: User
Tags setcookie

Recently in doing a shopping cart effect, in order to achieve the product browsing record effect But let me a variety of setbacks, to avoid later forget close-up this essay with you share, Hope Bo friends see after some harvest.

The first step: under a common JS file GetCookie ("Liulan"), the cookie name can be random, and then in determining whether the existence of this cookie, does not exist to create this cookie, the existence of a second cookie is assigned to Window.liu ( The entire window is the only variable). The code is as follows:

var L = GetCookie ("Liulan"); if (l) {Window.liu = L}else{setcookie ("Liulan", [],10);}

The second step: the first step of the JS file introduced into the product page, after the introduction of the product page to determine if there is window.liu, if there is Window.liu (Liu is an array at this time) to determine whether the length of Liu is greater than 0, if more than 0 to create an array and loop through the Liu, Add each of the Liu entries to the array and the items that dynamically create the record appear to the loaded place. The code is as follows:

if (Window.liu) {

var Liu = Window.liu;

if (liu.length>0) {

var arrobj = [];

for (var j=liu.length-1; j>=0; j--) {

var L = liu[J];

Arrobj.push ("<li id= '" "' >


$ (' ol '). HTML (Arrobj.join (")");//display when loading is separated by a null character

$ (' li ', ' ol '). "Click (function () {});//Set Click events for each Li



Step three: Add an event to the item when you browse the item. First use GetCookie to get the object of the product browsed (click to browse), to determine whether this cookie and Liu are present, if there are all traversed Liu's every item if one of Liu's data is the same ID and the product ID of the same will not let this cookie added to Liu, Then create Setcookie ("Liulian", Liu) if this cookie and Liu do not exist simultaneously on the Setcookie ("J",obj,10). The code is as follows:

A.onclick=function () {

var o = GetCookie ("J";

if (o && Liu) {

var F = true;

$ (Liu). each (function (I,item) {

if ( F = false;


if (f) {

if (liu.length >= 5) Liu.splice (0,1);

Liu.push (o);


Setcookie ("Liulan", Liu);

Document.location.href = "show.html?id=";


Setcookie ("J",obj,10);

document.location.href= "show.html?id=";



Note: If the code is wrong, welcome the great God and correct me.

Use the cookie in JS to realize the browsing record of the product.

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: 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.