正在开发一个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

JS制作一个通用的商城版历史浏览记录的更多相关文章

  1. php中如何实现网上商城用户历史浏览记录的代码

    /如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...

  2. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”

    这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...

  3. 用JS制作一个信息管理平台完整版

      前  言 JRedu 在之前的文章中,介绍了如何用JS制作一个实用的信息管理平台. 但是那样的平台功能过于简陋了,我们今天来继续完善一下. 首先我们回顾一下之前的内容.   1.JSON的基础知识 ...

  4. 使用Cookie实现用户商品历史浏览记录

    该功能分为四个模块: 1. 获取所有商品并以链接的形式显示 out.write("网站商品: <br/>"); Map<String, Book> book ...

  5. Django之使用redis缓存session,历史浏览记录,首页数据实现性能优化

    Redis缓存session 配置Django缓存数据到redis中 # diango的缓存配置 CACHES = { "default": { "BACKEND&quo ...

  6. Redis添加历史浏览记录

    参考资料 http://redisdoc.com/index.html http://redis-py.readthedocs.io/en/latest/#indices-and-tables 1.什 ...

  7. position:搜索框显示历史浏览记录

    absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", "righ ...

  8. destoon系统开发-最新利用浏览器的cookie 做历史浏览记录

      注意: 代码 放在要显示的为 (一般放在详情页),注意本教程不入库,直接利用浏览器的 cookie 缓存判断    <!--历史浏览记录 S--> <div class=&quo ...

  9. JS制作一个创意数字时钟

    通过js代码制作一个创意数字时钟 通过JS代码实现创意数字时钟效果如下:由数字化的卡通形象图片取代常规的数字显示当前实时北京时间.具体效果示例: 核心重点: (1)Date方法的初步了解 (2)构建模 ...

随机推荐

  1. MSSQL中循环

    1 declare @result table 2 ( 3 custid int, 4 ordermonth datetime, 5 qty int, 6 runqty int, 7 primary ...

  2. java基础讲解14-----IO

    package com.io; import java.io.File;import java.io.IOException; public class IoClass {        /**   ...

  3. 使用Parallel实现简单的并行操作

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  4. 关于new与=号创建对象的区别

    (1)先定义一个名为str的对String类的对象引用变量:String str: (2)[在[栈]中查找有没有存放值为"abc"的地址,如果没有,则开辟一个存放字面值为" ...

  5. 在Win7中IIS配置Asp.Net虚拟文件夹的方法及错误总结!

    在Win7中IIS配置Asp.Net虚拟文件夹的方法总结! 一.右键[站点].点击[加入虚拟文件夹]或[虚拟应用程序],笔者建议最好建立虚拟应用程序,由于这就跟一个站点差点儿相同.不用考虑路径问题. ...

  6. unity, rigidbody实现瞬移必须勾选is Kinematic

    用itween让一个绑定了rigidbody的沿曲线移动,当移动到末端时瞬间返回起始状态重新播放. 发现在不勾选isKinematic的情况下是不可能实现上述需求的.因为在动力学模式下任何物体的位置和 ...

  7. 点滴积累【JS】---JS小功能(checkbox实现全选和全取消)

    效果: 代码: <head runat="server"> <title></title> <script type="text ...

  8. swift -变量的定义与使用

    使⽤用let来声明常量,使⽤用var来声明变量. ⼀一个常量的值在编译时并不须要获取,可是你仅仅能为它赋值⼀一次.也就是说你能够⽤用常量来表⽰示这样⼀一个值:你仅仅须要决定⼀一次,可是须要使⽤用非常多 ...

  9. Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案

    Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...

  10. 小马哥课堂-统计学-t分布

    T distribution 定义 在概率论和统计学中,学生t-分布(t-distribution),可简称为t分布,用于根据小样本来估计 呈正态分布且方差未知的总体的均值.如果总体方差已知(例如在样 ...