jquery.cookie.js结合asp.net实现最近浏览记录

时间:2023-05-31 17:34:20

一、html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>最近浏览记录</title>
<script type="text/javascript" src="/scripts/jquery/jquery-1.10.2.min.js"></script>
<script src="/scripts/json.js" type="text/javascript"></script>
<script src="/scripts/jquery/jquery.cookie.js" type="text/javascript"></script>
<script>
(function ($) { $.history = function (e) {
var e = $.extend({
cookieName: "PrdIDCookie", //cookie名称
nid: 100, //最新访问的商品ID
N: 5 //设置cookie保存的浏览记录的条数
}, e)
var fn = {
cutstr:function(str, len, chr) {
var temp;
var icount = 0;
var patrn = /[^\x00-\xff]/;
var strre = "";
for (var i = 0; i < str.length; i++) {
if (icount < len - 1) {
temp = str.substr(i, 1);
if (patrn.exec(temp) == null) {
icount = icount + 1
} else {
icount = icount + 2
}
strre += temp
} else {
break
}
}
return typeof(chr)=="undefined"? strre:strre + chr;
},
//记录最近浏览过的商品
record: function () {
var _historyp;
if ($.cookie(e.cookieName) == null) { //cookie 不存在
$.cookie(e.cookieName, e.nid, { expires: 7, path: '/' }); //创建新的cookie,保存浏览记录
} else { //cookies已经存在
_historyp = $.cookie(e.cookieName); //获取浏览过的商品编号ID
};
var pArray = _historyp.split(','); //分解字符串为数组
_historyp = e.nid; //最新访问的商品编号放置载最前面
var count = 0; //判断是该商品编号是否存在于最近访问的记录里面
for (var i = 0; i < pArray.length; i++) {
if (pArray[i] != e.nid) {
_historyp = _historyp + "," + pArray[i];
count++;
if (count == e.N - 1) {
break;
}
}
}
$.cookie(e.cookieName, _historyp); //修改cookie的值
},
bind: function () { //获取最近浏览过的商品
var _historyp = "";
if ($.cookie(e.cookieName) != null) { //cookie 不存在
_historyp = $.cookie(e.cookieName); //获取浏览过的商品ID
}
if (_historyp == null && _historyp == "") {
return;
} else {
var prdIDs = []; //将商品ID以列表或数据的方式保存
var pArray = _historyp.split(',');
for (var i = 0; i < pArray.length; i++) {
if (pArray[i] != "") {
//alert(pArray[i]);
prdIDs.push(pArray[i]);
}
}
//--->请求商品详细详细...
$.post('/tools/submit_ajax.ashx?action=get_history_log', { id_arr: prdIDs.toString() }, function (data) {
var data = JSON.parse(data);
var list = '';
if (data) {
for (var i = 0; i < data.length; i++) {
list += '<li><a href="/fy/show/' + data[i].id + '.html"><font style="color:#0041d9">' + fn.cutstr(data[i].title,20) + '</font>-<font style="color:#ff6600">' + data[i].price + '元/平米.天</font></a></li>';
}
}
$("#history_log").html(list);
});
}
}
}
return fn;
}
})(jQuery)
$.history({
cookieName: "PrdIDCookie",
nid: 1628,
N: 5
}).record();
$.history().bind();
</script>
</head> <body>
<ul id="history_log"></ul>
</body>
</html>

asp.net处理代码

private void get_history_log(HttpContext context)
{
string id = DTRequest.GetFormString("id_arr");
DataTable dt = new BLL.article().Getdata_List("housing", 5, "id in(" + id + ")", "sort_id asc").Tables[0];
var list = new List<object>();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
list.Add(new { id = dr["id"], title = dr["title"], price = dr["price"] });
}
context.Response.Write(new JavaScriptSerializer().Serialize(list));
return;
}