JS制作一个通用的商城版历史浏览记录
正在开发一个b2c的国外商城,昨天做了一个历史浏览记录发出来跟大家分享一下.
JS:
//cookie相关函数
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function setCookie(name, value) {
var exp = new Date();
exp.setTime(exp.getTime() + 3600000000);
document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();
}
function glog(evt) {
var evt = evt ? evt : window.event;
var srcElem = (evt.target) ? evt.target : evt.srcElement;
try {
while (srcElem.parentNode && srcElem != srcElem.parentNode) {
if (srcElem.tagName && srcElem.tagName.toUpperCase() == "A") {
addressCookie = srcElem.href;
var ProductId = addressCookie.substr(addressCookie.indexOf("id=") + 3);
var imgUrl = document.getElementById("img_" + ProductId + "").src;
var productNameTitle = document.getElementById("Name_" + ProductId + "").text;
var productName;
if (productNameTitle.length > 28) {
productName = productNameTitle.substr(0, 25) + "...";
}
else {
productName = productNameTitle;
}
var productPrice = document.getElementById("Price_" + ProductId + "").textContent;
address = srcElem.href + "+" + ProductId + "+" + imgUrl + "+" + productNameTitle + "+" + productName + "+" + productPrice + "|";
wlink = address;
old_info = getCookie("history_info");
var insert = true;
if (old_info == null) {//判断cookie是否为空
insert = true;
}
else {
var old_link = old_info.split("|");
for (var j = 0; j <= 5; j++) {
if (old_link[j].indexOf(addressCookie) != -1) //存在这个cookie
insert = false;
if (old_link[j] == "null")
break;
}
}
if (insert) {
wlink += getCookie("history_info");
setCookie("history_info", wlink);
history_show().reload();
break;
}
else {
var old_link1 = old_info.split("|");
var length = old_link1.length;
var newcookie = '';
for (var j = 0; length <= 6 ? j <= length - 1 : j <= 5; j++) {
if (old_link1[j].indexOf(addressCookie) == -1) {
if (j == length - 1 || j == 5) {
newcookie = newcookie + old_link1[j]
}
else {
newcookie = newcookie + old_link1[j] + '|'
}
}
}
newcookie = wlink + newcookie
setCookie("history_info", newcookie);
history_show().reload();
break;
}
}
srcElem = srcElem.parentNode;
}
}
catch (e) { }
return true;
}
function history_show() {
var history_info = getCookie("history_info");
if (history_info != null) {
history_arg = history_info.split("|");
var content = "";
for (var i = 0; history_arg.length >= 6 ? i<=5 : i<=history_arg.length - 1; i++) {
if (history_arg[i] != "null") {
historyArry = history_arg[i].split("+");
content += "<li><div class=\"products_scan_pic\"><a onclick=\"glog(event)\" href=\"ProductsInfo.aspx?id=" + historyArry[1] + "\"><img id=\"img_" + historyArry[1] + "\" src=\"" + historyArry[2] + "\" onerror=\"javascript:this.src='images/Product/nophoto.jpg'\" width=\"47\" height=\"47\" /></a></div><div class=\"products_scan_txt\"><a href=\"ProductsInfo.aspx?id=" + historyArry[1] + "\" onclick=\"glog(event)\" id=\"Name_" + historyArry[1] + "\" title=\"" + historyArry[3] + "\">" + historyArry[4] + "</a><br/><div id=\"Price_" + historyArry[1] + "\"><div class=\"products_seller_t2\">" + historyArry[5] + "</div></div></div></li>";
}
}
document.getElementById("history").innerHTML = content;
}
else {
document.getElementById("history").innerHTML = "<span style=\"color:#DE8500\">You have no recently viewed items or searches</span>";
}
}
//清空cookies
function ClearHistory() {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = "history_info=;expires=" + exp.toGMTString() + "";
document.getElementById("history").innerHTML = "<span style=\"color:#DE8500\">Cleared view history successfully</span>";
}
前台调用方式:
<script type="text/javascript" defer="defer">history_show();</script>
效果图:
清除历史浏览记录:
<div class="products_scan_title"><span><a href="#" onclick="ClearHistory()">Clear History</a></span>Your last viewed items</div>
在这个项目之前我开发了一个类似"暴走漫画"搞笑图片制作的网站,现在网上很流行的搞笑图片,大多都来自那个网站,它的图片制作是用flash制作的,由于公司的某些策划和原因,我用asp.net开发了一个类似的,不过总感觉有瑕疵,这也是程序和flash的区别,无解!!. 网址:www.banmx.com 也希望有懂Gid.用asp.net开发过类似项目的朋友能给点意见. QQ357253950