0

我在下面有这个脚本,我用它来设置和阅读使用 JavaScript 浏览的最后 5 个页面。客户端不希望呈现任何重复的 URL/文本,但我到目前为止所尝试的一切都没有运气。

也许我做错了。任何帮助,将不胜感激。

// 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);
    }
4

1 回答 1

0

您可以尝试将值作为 JSON 存储在一个 cookie 中。

// 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();
};
于 2012-06-22T20:47:59.383 回答