Thursday, August 13, 2015

Show Last Visited Pages from Cookie using Javascript & JQuery Or Store User Latest Visit Pages in Cookie

You need to just add this js code on you page and provide the class id where you wanted to show your result only, rest of the thing my code will do

/************By Murli on 12 Aug 2015 ********************/
/*******Purpose: To show last 4 visited pages using object & array on the site***************/
/********* http://murlid05.blogspot.in/ *****************/

var template = { Title: "", Desc: "Temp Name", Url: "" };

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString() + "; path=/";
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

function checkCookie(PageTitle, PageDescription, PageUrl) {

    var newArray = new Array(); /*Temp Array to generate cookie*/
    var tempObject = ""; /*To hold the already exists page data*/
    var arrObject;  /*To store cookie value in a object*/

    if (getCookie("RecentViewList") != '') {
        arrObject = $.parseJSON(getCookie("RecentViewList")); /* Convert string to object */
    }

    if (arrObject != null) {
        $.each(arrObject, function (index, value) {
            if (PageUrl == value.Url) {
                tempObject = { Title: PageTitle, Desc: PageDescription, Url: PageUrl };
            } else {
                newArray.push({ Title: value.Title.toString(), Desc: value.Desc.toString(), Url: value.Url.toString() });
            }
        });
    }

    if (tempObject != "") {
        newArray.push(tempObject);
    } else {
        if (newArray.length > 3) newArray.splice(0, 1); /*Remove most recent value from array*/
        tempObject = { Title: PageTitle, Desc: PageDescription, Url: PageUrl };
        newArray.push(tempObject);
    }

    var stringObj = JSON.stringify(newArray); /*Convert Array to JSON formated string*/
    setCookie("RecentViewList", stringObj, 1);

    FnShowonPage(); /*Draw on a page*/
}


function FnShowonPage() {
    var arr = getCookie("RecentViewList");
    var Description = "";
    arr = $.parseJSON(arr);
    var HtmlData = "<a class='closerightFloater' href='javascript:;'></a><ul class='browserVisitlist'>";
    $.each(arr, function (index, value) {
        Description = (value.Desc.length > 120) ? (value.Desc.substring(0, 120) + "....") : value.Desc;
        HtmlData += "<li><a class='recentLinks' title=" + value.Title + "' href='" + value.Url + "' >" + value.Title.substring(0, 25) + "</a><p>" + Description + "</p></li>";
    });
    HtmlData += "</ul>";
    $(".rightPanelbrowsebig").html(HtmlData);
}


$(document).ready(function () {
    var PageDescription = $("meta[name='description']").attr("content");
    var PageTitle = document.title.replace("é","e");
    var PageUrl = window.location.href;

    if ((typeof document.title !== "undefined") && (typeof $("meta[name='description']").attr("content") !== "undefined")) {
        PageDescription = (typeof $("meta[name='description']").attr("content") !== "undefined") ? PageDescription : "No";
        // alert(PageDescription + "  ::Under On load:   " + PageTitle + "  ::::     " + PageUrl);
        checkCookie(PageTitle.toString(), PageDescription.toString(), PageUrl.toString());
    }
    else {
        //alert("somthing wrong   :   " + PageTitle);
    }
})

1 comment:

  1. Hi, how i can set to show last 3 visited pages and not 4?

    ReplyDelete