View Full Version : Previous Page Variable

06-17-2007, 04:52 AM
I need a variable to be available on each page of my website that indicates which page the user has just come from. So if the user in now on the "contact" page, and came there from "home", the variable should read "home", or some number corresponding to "home". How would I do this with JS?

I thought about having each site page declare a JS variable, and then pass it to the next page. How would I code this variable passage between pages?

Philip M
06-17-2007, 08:53 AM
Set a cookie.

06-18-2007, 01:02 PM
Set a cookie.

No good if user has cookies disabled.

This script displays a breadcrumb trail. Useless though if javascript is turned off.

function breadcrumbs(){
sURL = new String;
bits = new Object;
var x = 0;
var stop = 0;
var output = "<a href=\"/\">Home</a> > ";
sURL = location.href;
sURL = sURL.slice(8,sURL.length);
chunkStart = sURL.indexOf("/");
sURL = sURL.slice(chunkStart+1,sURL.length)
chunkStart = sURL.indexOf("/");
if (chunkStart != -1){
bits[x] = sURL.slice(0,chunkStart)
sURL = sURL.slice(chunkStart+1,sURL.length);
stop = 1;
for(var i in bits){
output += "<a href=\"";
output += "../";
output += bits[i] + "/\">" + bits[i] + "</a> > ";
document.write(output + document.title);


06-18-2007, 01:10 PM
You could do this via the link to each page.

If you set a variable within each page identifying it, such as:

var pagename = "home";

Then in your links, you can append this to the linked filename:

<a href="contacts.htm?"+pagename >

Ideally, you would do this by manipulating the DOM by modifying the atrtribute "href" for all <a> tags.

Within each page, you can then extract the Query String:

var source = location.search;

This will set 'source' to "?home" so you will need to remove the question mark.