2012-06-22 8 views
0

JavaScript를 사용하여 마지막으로 본 5 개의 페이지를 설정하고 읽는 데 사용하는이 스크립트는 아래에 있습니다. 클라이언트는 중복 된 URL/텍스트 렌더링을 원하지 않지만 지금까지 시도한 내용과 관련하여 어떤 행운을 빕니다.Javascript에서 읽은 쿠키에서 중복 항목 제거

어쩌면 나는 완전히 잘못 될 것입니다. 어떤 도움을 주시면 감사하겠습니다.

// Set read, set & delete cookie functions------------------------------------------------------------------------- 

    function getCookie (cookie_name){ 
     var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); 
     if (results) { 
     return (unescape (results[2])); 
     } else { 
     return null; 
     } 
    } 

    function setCookie (name,value,expiredays){ 
     var exdate = new Date(); 
     exdate.setDate(exdate.getDate()+expiredays); 
     document.cookie = name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString()); 
    } 

    function delete_cookie (cookie_name) { 
     var cookie_date = new Date (); // current date & time 
     cookie_date.setTime (cookie_date.getTime() - 1); 
     document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString(); 
    } 

// Set last 5 visited pages cookies -------------------------------------------------------------------------------- 

    tlvp_the_last_visited_pages(); 

    // function to get info from cookies for last five pages. 
    // Needs to be seperate from getCookie function for parsing reasons. 
    function fetchCookie(name){ 
     if(document.cookie.length>0){ 
      start = document.cookie.indexOf(name+"="); 
      if(start!=-1){ 
      start = start+name.length+1; 
      end = document.cookie.indexOf(";",start); 
      if(end==-1){ 
       end = document.cookie.length; 
      } 
      return unescape(document.cookie.substring(start,end)); 
      } 
      } 
     return ""; 
    } 

    function tlvp_the_last_visited_pages(){ 
     tlvp_div = document.getElementById('the_last_visited_pages'); 

     if(tlvp_pages_count > 0){ 
      for(var i = tlvp_pages_count; i >= 0; i--){ 
       if(i > 0){ 
        setCookie("tlvp_visited_page"+i+"_link",fetchCookie("tlvp_visited_page"+(i-1)+"_link"),tlvp_expiredays); 
        setCookie("tlvp_visited_page"+i+"_title",fetchCookie("tlvp_visited_page"+(i-1)+"_title"),tlvp_expiredays); 
       } else { 
        setCookie("tlvp_visited_page"+i+"_link",document.URL,tlvp_expiredays); 
        setCookie("tlvp_visited_page"+i+"_title",document.title,tlvp_expiredays); 
       } 
      } 
     } 

     // This is where the code is created for the div...  
     tlvp_last_visited_pages_title = document.createElement("div"); 
     tlvp_last_visited_pages_title.className = "tlvp_title"; 
     tlvp_last_visited_pages_title_text = document.createTextNode(tlvp_title); 
     tlvp_last_visited_pages_title.appendChild(tlvp_last_visited_pages_title_text); 
     tlvp_div.appendChild(tlvp_last_visited_pages_title);  
     tlvp_last_visited_pages_content = document.createElement("div"); 
     tlvp_last_visited_pages_content.className = "tlvp_content"; 

     // Loops through the cookies and creates text links...   
     for(var i=1; i<=tlvp_pages_count; i++){ 
      var e = fetchCookie("tlvp_visited_page"+i+"_link"); 
      if (e != "") { 
       tlvp_visited_page_line = document.createElement("p"); 
       tlvp_visited_page_a = document.createElement("a"); 
       tlvp_visited_page_a.href = getCookie("tlvp_visited_page"+i+"_link"); 
       tlvp_visited_page_text = document.createTextNode(getCookie("tlvp_visited_page"+i+"_title")); 
       tlvp_visited_page_a.appendChild(tlvp_visited_page_text); 
       tlvp_visited_page_line.appendChild(tlvp_visited_page_a); 
       tlvp_last_visited_pages_content.appendChild(tlvp_visited_page_line); 
      } 
     } 

     tlvp_div.appendChild(tlvp_last_visited_pages_content); 
    } 

답변

0

값을 JSON으로 하나의 쿠키에 저장할 수 있습니다.

// Set read, set & delete cookie functions------------------------------------------------------------------------- 

function getCookie (cookie_name){ 
    var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); 
    if (results) { 
     return (unescape (results[2])); 
    } else { 
     return null; 
    } 
} 

function setCookie (name,value,expiredays){ 
    var exdate = new Date(); 
    exdate.setDate(exdate.getDate()+expiredays); 
    document.cookie = name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString()); 
} 

function delete_cookie (cookie_name) { 
    var cookie_date = new Date (); // current date & time 
    cookie_date.setTime (cookie_date.getTime() - 1); 
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString(); 
} 

// Set last 5 visited pages cookies -------------------------------------------------------------------------------- 

function last_visited() { 
    var max_urls = 5; 
    var cookie = getCookie("last_visited"); 
    var url = window.location.href; 

    // Get the JSON cookie or a new array. 
    var urls = (cookie != null) ? JSON.parse(cookie) : []; 

    // Build new_urls out of history that is not this url. 
    var new_urls = []; 
    for (var i=0; i < urls.length; i++) {  
     if (urls[i].url != url) { 
      new_urls.push(urls[i]); 
     } 
    } 

    // remove the last item if the array is full. 
    if (new_urls.length == max_urls) { 
     new_urls.pop(); 
    } 

    // Add this url to the front. 
    new_urls.unshift({url: url, title: document.title}); 

    // Save it 
    setCookie("last_visited", JSON.stringify(new_urls),1); 

    // Create html 
    var html = "<ul>\n"; 
    for (var i = 0; i < new_urls.length; i++) { 
     html += "<li><a href=\"" + new_urls[i].url + "\">" + new_urls[i].title + "</a></li>\n" 
    } 
    html += "</ul>\n"; 

    // Render html. 
    var el = document.getElementById("last_visited"); 
    el.innerHTML = html; 
} 

window.onload = function() { 
    last_visited(); 
};