zoukankan      html  css  js  c++  java
  • JAVASCRIPT+COOKIE 显示最近浏览文章

    JAVASCRIPT+COOKIE 显示最近浏览文章,需要考虑到按浏览的先后时间排序,如果最近浏览记录中有与当前重复的则替换存在的记录时间,并将其提到最前一条。然后再进行排序。

    在正文页面的调用代码:

    <div>
    <ul>
    <script src="rencent_article.js"></script>
    </ul>
    </div>

    ==========rencent_article.js文件源码=======================

    //DISPLAY RECENT ARTICLE
    //AUTHOR ZTOTEM
    //DATE 2006-09-28

    function article_list(cookieName,a_title){
     
     var isdone="none";
        if(GetCookie("EMARTICLE9")!=null){   
     for(i=0;i<10;i++){                           
       arr_temp_check=GetCookie("EMARTICLE"+i).split("@#$");
        if(arr_temp_check.length>1){                
       temp_check=arr_temp_check[1]; 
     
       arr_a_title=a_title.split("@#$")             
       temp_arr_a_title=arr_a_title[0]; 
      
       if(temp_check==temp_arr_a_title){ 
        SetCookie("EMARTICLE"+i,a_title);
        isdone="ok";
        //break;
        }
       }
      }
    }

      if(isdone=="none"){
       replacecookie(cookieName,a_title);
       new_article=GetCookie(cookieName);
       for(i=0;i<9;i++){
        k=i+1;
         //if(GetCookie('EMARTICLE'+k)==null){str_replace=""}else{str_replace=GetCookie('EMARTICLE'+k)}
        replacecookie("EMARTICLE"+i,GetCookie('EMARTICLE'+k));
       }
         SetCookie("EMARTICLE9",new_article);
      }


     
    function SetCookie(cookieName,a_title) {
       var c_time=new Date();
       var s_month=(c_time.getMonth()+1).toString();
       var s_day=c_time.getDate().toString();
       var s_hour=c_time.getHours().toString();
       var s_minutes=c_time.getMinutes().toString();
       var s_seconds=c_time.getSeconds().toString();
      
       if(s_month.length<2){s_month="0"+s_month;}
       if(s_day.length<2){s_day="0"+s_day;}
       if(s_hour.length<2){s_hour="0"+s_hour;}
       if(s_minutes.length<2){s_minutes="0"+s_minutes;}
       if(s_seconds.length<2){s_seconds="0"+s_seconds;}
       
        var strtime=s_month+"-"+s_day+" "+s_hour+":"+s_minutes+":"+s_seconds
        var expires = new Date ();
       
        expires.setTime(expires.getTime() + 31 * (24 * 60 * 60 * 1000));
       // document.cookie = cookieName + "=" + strtime+ "@#$"+escape(a_title) +"; expires=" + expires.toGMTString();
       document.cookie = cookieName + "=" + strtime+ "@#$"+escape(a_title) +"; expires=" + expires.toGMTString();
    }

    function replacecookie(cookieName,a_title){
        var expires = new Date ();
        expires.setTime(expires.getTime() + 31 * (24 * 60 * 60 * 1000));
        document.cookie = cookieName + "=" + escape(a_title)+"; expires=" + expires.toGMTString();;
    }

    function GetCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1)
        {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
        {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));

     

    article_list('EMARTICLE0',document.title+'@#$'+window.location.href);
     listArticle=new Array();
     for(i=0;i<10;i++){
     listArticle[i]=GetCookie('EMARTICLE'+i);
     // document.writeln(i+"="+listArticle[i]) ;
     //  document.writeln("<br>") ;
    }
     
     listArticle.sort();
     var jj=new Array();
       for(i=listArticle.length-1;i>=0;i--){
     if(listArticle[i]!="null"){
      str_temp=listArticle[i].split("@#$");
      jj[i]=str_temp[1];
      // document.writeln(listArticle[i]);
     document.writeln("<li> <a href='"+str_temp[2]+"'>"+str_temp[1].replace(' - 要替换的字符','').substr(0,12)+"</a></li>") ;
      //document.writeln("<br>") ;
    }

       }
     

    ===============================================



    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1301517

  • 相关阅读:
    [CodeForces]Codeforces Round #429 (Div. 2) ABC(待补)
    About Me
    2018-06-14
    Codeforces Codeforces Round #484 (Div. 2) E. Billiard
    Codeforces Codeforces Round #484 (Div. 2) D. Shark
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
    Codeforces Avito Code Challenge 2018 D. Bookshelves
    Codeforces Round #485 (Div. 2) D. Fair
    Codeforces Round #485 (Div. 2) F. AND Graph
  • 原文地址:https://www.cnblogs.com/zwl12549/p/977734.html
Copyright © 2011-2022 走看看