jQuery. cookie. js Implementation Record Recent Browsed Item Feature Example
- 2021-07-13 04:23:28
- OfStack
This article illustrates the jQuery. cookie. js implementation of recording recently browsed merchandise functionality. Share it for your reference, as follows:
1. jquery. cookie. js
/*jquery.cookie.js */
jquery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
2. Realize the recently browsed products
var cookieName = "PrdIDCookie"; //cookie Name
var nid; // The latest products visited ID
var N = 5; // Settings cookie Number of browsing records saved
// Record recently browsed products
function HistoryRecord() {
var historyp;
nid = $("#PrdID").val();
if (nid == null || nid == "") {
return;
}
// Judge whether it exists or not cookie
if ($.cookie(cookieName) == null) //cookie Nonexistent
{
// Create a new cookie, Save browsing records
$.cookie(cookieName, nid, { expires: 7, path: '/' });
}
else //cookies Already exists
{
// Get the browsed item number ID
historyp = $.cookie(cookieName);
};
// Decompose a string into an array
var pArray = historyp.split(',');
// The most recently visited item number is placed in the front
historyp = nid;
// Determine whether the item number exists in the recently accessed record
var count = 0;
for (var i = 0; i < pArray.length; i++) {
if (pArray[i] != nid) {
historyp = historyp + "," + pArray[i];
count++;
if (count == N - 1) {
break;
}
}
}
// Modify cookie Value of
$.cookie(cookieName, historyp);
}
// Get recently browsed products
function BindHistory() {
var historyp = "";
if ($.cookie(cookieName) != null) //cookie Nonexistent
{
// Get browsed goods ID
historyp = $.cookie(cookieName);
}
if (historyp == null && historyp == "") {
return;
}
else
{
var prdIDs = []; // Put merchandise ID Save as a list or data
var pArray = historyp.split(',');
for (var i = 0; i < pArray.length; i++) {
if (pArray[i] != "") {
//alert(pArray[i]);
prdIDs.push(pArray[i]);
}
}
//---> Request commodity details ...
}
}
For more readers interested in jQuery related contents, please check the topics of this site: "Summary of cookie Operation Skills of jQuery", "Summary of jQuery Extension Skills", "Summary of Common Plug-ins and Usage of jQuery", "Summary of Operation Skills of jQuery Table (table)", "Summary of Common Classic Special Effects of jQuery" and "Summary of Usage of jquery Selector"
I hope this article is helpful to everyone's jQuery programming.