Jquery 最近瀏覽過的商品的功能實現(xiàn)代碼
更新時間:2010年05月14日 17:21:44 作者:
Jquery 最近瀏覽過的商品的功能實現(xiàn)代碼,需要的朋友可以參考下。
<script type="text/javascript" src="JS/jquery.js"></script> //與Prototype 類似的框架
<script type="text/javascript" src="JS/JCookie.js"></script> //插件
/*JCookie.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;
}
};
<div id="latestp"></div>
<script type="text/javascript">
var historyp;
//最新訪問的商品編號ID
var nid="新商品編號ID";
//設(shè)置cookie保存的瀏覽記錄的條數(shù)
var N=10;
var count=0;
//判斷是否存在cookie
if($.cookie('smile1314h')==null) //cookie 不存在
{
//創(chuàng)建新的cookie,保存瀏覽記錄
$.cookie('smile1314h',nid,{expires:7,path:'/'});
//$.cookie('smile1314h',nid,{expires:7,path:'/',domain:'smile1314.com',secure:true});
}
else //cookies已經(jīng)存在
{
//獲取瀏覽過的商品編號ID
historyp=$.cookie('smile1314h');
var check_result = $('#latestp');
check_result.html('<img src=/CSS/Image/Loading.gif style=/"margin-left:40px;;/">');
//ajax 根據(jù)產(chǎn)品編號獲取信息列表
$.ajax({ //一個Ajax過程
type: "get",
url : "/Comm/getLatestProduct.ashx",
dataType:'html',
data: "P_Id="+historyp,
success: function(json){
check_result.html("");
check_result.html(json);
}});
//分解字符串為數(shù)組
var pArray=historyp.split(',');
//最新訪問的商品編號放置載最前面
historyp=nid;
//判斷是該商品編號是否存在于最近訪問的記錄里面
for(var i=0;i<pArray.length;i++)
{
if(pArray[i]!=nid)
{
historyp=historyp+","+pArray[i];
count++;
if(count==N-1)
{
break;
}
}
}
//修改cookie的值
$.cookie('smile1314h',historyp);
}
</script>
打包下載
<script type="text/javascript" src="JS/JCookie.js"></script> //插件
復(fù)制代碼 代碼如下:
/*JCookie.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;
}
};
復(fù)制代碼 代碼如下:
<div id="latestp"></div>
<script type="text/javascript">
var historyp;
//最新訪問的商品編號ID
var nid="新商品編號ID";
//設(shè)置cookie保存的瀏覽記錄的條數(shù)
var N=10;
var count=0;
//判斷是否存在cookie
if($.cookie('smile1314h')==null) //cookie 不存在
{
//創(chuàng)建新的cookie,保存瀏覽記錄
$.cookie('smile1314h',nid,{expires:7,path:'/'});
//$.cookie('smile1314h',nid,{expires:7,path:'/',domain:'smile1314.com',secure:true});
}
else //cookies已經(jīng)存在
{
//獲取瀏覽過的商品編號ID
historyp=$.cookie('smile1314h');
var check_result = $('#latestp');
check_result.html('<img src=/CSS/Image/Loading.gif style=/"margin-left:40px;;/">');
//ajax 根據(jù)產(chǎn)品編號獲取信息列表
$.ajax({ //一個Ajax過程
type: "get",
url : "/Comm/getLatestProduct.ashx",
dataType:'html',
data: "P_Id="+historyp,
success: function(json){
check_result.html("");
check_result.html(json);
}});
//分解字符串為數(shù)組
var pArray=historyp.split(',');
//最新訪問的商品編號放置載最前面
historyp=nid;
//判斷是該商品編號是否存在于最近訪問的記錄里面
for(var i=0;i<pArray.length;i++)
{
if(pArray[i]!=nid)
{
historyp=historyp+","+pArray[i];
count++;
if(count==N-1)
{
break;
}
}
}
//修改cookie的值
$.cookie('smile1314h',historyp);
}
</script>
打包下載
您可能感興趣的文章:
- Android仿淘寶商品瀏覽界面圖片滾動效果
- 類似天貓商品詳情隨瀏覽器移動的示例代碼
- Android時光軸實現(xiàn)淘寶物流信息瀏覽效果
- PHP實現(xiàn)采集抓取淘寶網(wǎng)單個商品信息
- JQuery實現(xiàn)的購物車功能(可以減少或者添加商品并自動計算價格)
- PHP 類商品秒殺計時實現(xiàn)代碼
- asp.net下使用jQuery.AutoComplete完成仿淘寶商品搜索自動完成功能(改進了鍵盤上下選擇體驗)
- 基于jquery的商品展示放大鏡
- 仿當當網(wǎng)淘寶網(wǎng)等主流電子商務(wù)網(wǎng)站商品分類導(dǎo)航菜單
- JAVAEE model1模型實現(xiàn)商品瀏覽記錄(去除重復(fù)的瀏覽記錄)(一)
相關(guān)文章
jQuery實現(xiàn)統(tǒng)計輸入文字個數(shù)的方法
這篇文章主要介紹了jQuery實現(xiàn)統(tǒng)計輸入文字個數(shù)的方法,涉及jQuery操作鼠標事件及dom元素的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03一個很簡單的jquery+xml+ajax的無刷新樹結(jié)構(gòu)(無css,后臺是c#)
一個很簡單的jquery+xml+ajax的無刷新樹結(jié)構(gòu)(無css,后臺是c#)2010-06-06jquery之a(chǎn)jaxfileupload異步上傳插件(附工程代碼)
在處理文件上傳時需要使用到文件的異步上傳,這里使用Jquery Ajax File Uploader這個組件,服務(wù)器端采用struts2來處理文件上傳2013-04-04jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能示例
這篇文章主要介紹了jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能,結(jié)合實例形式分析了基于jQuery的倒計時操作功能實現(xiàn)方法,涉及jQuery表單提交、驗證、正則操作等技巧,需要的朋友可以參考下2017-01-01關(guān)于jQuery中.attr()和.prop()的問題探討
寫了幾句代碼在ie8上能正常運行,chrome和ff卻不行,prop()方法適用于Boolean值的屬性,添加屬性名稱該屬性就會生效應(yīng)該使用.prop()者兩點就是他們的區(qū)別吧2013-09-09鋒利的jQuery 要點歸納(三) jQuery中的事件和動畫(上:事件篇)
鋒利的jQuery 要點歸納 jQuery中的事件和動畫(上:事件篇)2010-03-03jQuery訪問json文件中數(shù)據(jù)的方法示例
這篇文章主要介紹了jQuery訪問json文件中數(shù)據(jù)的方法,結(jié)合實力形式分析了jQuery事件響應(yīng)及json文件的加載、讀取、遍歷等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01