分享一段JS代码,个人感觉很使用:网页浏览历史记录

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv=content-type content="text/html; charset=gb2312" />
  5. <title>小林网 - Xiaolin.Net</title>
  6. </head>
  7. <body>
  8. <fieldset id="xiaolin" style="border:1px red solid; width:300px; height:400px;line-height:25px; color:blue">
  9. <legend style="color:red">您最近浏览过的新闻 </legend>
  10. 未找到您的最近浏览记录
  11. </fieldset>
  12. <script type="text/javascript">
  13. // <![CDATA[
  14. if(navigator.cookieEnabled){//判断是否支持Cookie
  15.      var tag = document.getElementById("xiaolin");//输出结果DIV id
  16.      var N = 10;//太长标题截取汉字字数
  17.      var listCount = 10;//记录和显示最近几条
  18.      var myTitle = escape(document.title) + "^";//取标题
  19.      var myUrl = escape(location.pathname) + "$";//取地址
  20.      var expTime = new Date(new Date().setDate(new Date().getDate() + 500));//设定过期时间为500天
  21.      var edp = "|; expires=" + expTime.toGMTString() + "; path=/"; //; domain=xiaolin.net //组合Cookie参数
  22.      var allCookie = document.cookie; //取出全部Cookie
  23.      if(allCookie.indexOf("news=") != -1 || allCookie.indexOf("$|") != -1){//判断是否为第一次浏览
  24.          var myCookieStart = allCookie.indexOf("news=") + "news=".length;
  25.          var myCookieEnd = allCookie.indexOf("$|",myCookieStart);
  26.          var myCookieall = unescape(allCookie.substring(myCookieStart,myCookieEnd));
  27.          var myCookie = myCookieall.split("$");
  28.          var myCookieTit = new Array();
  29.          var myCookieUrl = new Array();
  30.          for(var i = 0; i < myCookie.length; i++){
  31.              var myCookieOne = myCookie[i].split("^");
  32.              myCookieTit[i] = myCookieOne[0];
  33.              myCookieUrl[i] = myCookieOne[1];
  34.          }
  35.          var tagDat = "";
  36.          for(var i =myCookieTit.length - 1; i >= 0; i--){//从先到后顺序取得结果值
  37.              var textCount = myCookieTit[i].replace(/[^\x00-\xff]/g,"aa").length;
  38.              if(textCount <= N*2){
  39.                  texts = myCookieTit[i];
  40.              }else{
  41.                  for(var a = N; a < textCount; a++){
  42.                      texts = myCookieTit[i].substr(0,a);
  43.                      if(texts.replace(/[^\x00-\xff]/g,"aa").length >= N*2){
  44.                          texts += "..."
  45.                          break;
  46.                      }
  47.                  }
  48.              }
  49.              tagDat += "<a href=\"" + myCookieUrl[i] + "\"" + " title=\"" + myCookieTit[i] + "\">" + texts + "</a><br>"
  50.          }
  51.      tag.innerHTML =tagDat;//输出结果给页面div
  52.      var newCookie = "";//************************************************
  53.      if(myCookie.length < listCount){
  54.          for(i in myCookie){
  55.              if(myCookieTit[i] == document.title) continue;//去除重复
  56.              newCookie += escape(myCookieTit[i]) + "^" + escape(myCookieUrl[i]) + "$" ;
  57.          }
  58.      }else{
  59.          for(var i = 1; i < listCount; i++){
  60.              if(myCookieTit[i] == document.title) continue;
  61.              newCookie += escape(myCookieTit[i]) + "^" + escape(myCookieUrl[i]) + "$" ;
  62.          }
  63.      }
  64.      document.cookie = "news=" + newCookie + myTitle + myUrl + edp;//**********
  65.      }else{//
  66.          document.cookie = "news="+ myTitle + myUrl + edp;  
  67.      }
  68. }else{//不支持Cookie的处理
  69.      tag.innerHTML="您浏览器关闭了cookie功能,不能为您自动保存最近浏览过的网页。"
  70. }
  71. // ]]>
  72. </script>
  73. </body>
  74. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值