Forum Moderators: open

Message Too Old, No Replies

Displaying browser history with Javascript

Trying to develop (or find) a "You've seen these pages on my site" script

         

MatthewHSE

8:07 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sure I've seen a javascript somewhere - probably one of the IE-only things - that would show the most recent x page titles from the browser history. I'm working on a redesign of my main site, and I'd like to have a navigational aid in the form of a small, discreet, mouseover menu that will display the last ten or twenty pages the visitor has viewed, from my domain only, and link to those pages. Unfortunately I can't find how to do this, although as I said, I'm sure I've seen something like this before. Any ideas how I can use JS to access and display this information to the visitor?

Thanks,

Matthew

RonPK

5:37 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to store a string with the URL and the title of the current page into an array. Than, store the array as a string in a cookie.

On arriving on the next page, the cookie is read, the string split into an array, you loop through the array and put the stored links in a variable that you can later display with document.write(). Than the URL and the title of the new page are stored, and if necessary the array will be trimmed to 10 or 20 items by popping the oldest item of the wagon. Next, store the array as a string in the cookie.

Is that what you were thinking of?

MatthewHSE

6:19 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would do it, yes. I hadn't thought of doing it that way, as I thought there was a way of grabbing it from the browser history. However, the method you describe would work.

Now to try to figure out how to do it - I'm learning enough JavaScript that I think I'll get it eventually, but if someone could point out a few of the functions, objects or methods I'll need (are those even the right terms? Not sure...) I'd really appreciate it! ;)

Thanks again,

Matthew

RonPK

8:29 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Browser history doesn't contain the pages' titles..

I'll try to write a bit of code that may get you going. The worst and ugliest part is reading of the cookie.


// current page info
var curPage = '<a href="' + location.href + '">' + document.title + '<\/a>';
// set max # of pages in history list
var maxPages = 10;
// let's call our page info myhistory, and the page element histlist
var myhistory, histlist;
// check if cookie with past pages already exists
if (document.cookie.indexOf('myhistory=') > -1) { // myhistory exists
// a cookie string basically looks like myhistory=bladibla;othercookie=foobar;
// so the start of myhistory's value is at
var startpos = document.cookie.indexOf('myhistory=') + 10;
// now the endpos:
var endpos = document.cookie.indexOf(';', startpos);
// endpos == -1 if our cookie is the last one
if (endpos == -1)
endpos = document.cookie.length;
// now we can finally extract the value
myhistory = document.cookie.substring(startpos, endpos);
// unescape it and split it into an array
// we'll use ### as delimiter
myhistory = unescape(myhistory);
myhistory = myhistory.split('###');
// loop through this array and put links in histlist
// pro memoriam...
} else {
// make myhistory an empty array;
myhistory = new Array();
}
// now insert the current page at the start of the array
// [deity] knows why they call this method unshift...
myhistory.unshift(curPage);
// drop the last item if list is too long
if (myhistory.length > maxPages)
myhistory.pop();
// now store the joined items of the array in the cookie.
document.cookie = escape(myhistory.join('###'));

Completely untested...