views:

307

answers:

1

I'm playing around with using some javascript to add extra functionality to a back button on my website. Right now I have a set of javascript that looks like this:

$(".back-link a").live("click", function(){
    history.go(-1);
    return false;
});

Now, it works great but I'm trying to make it as bulletproof as possible and one issue I foresee is that if someone lands on a project page and hasn't come to it via my home page then going back in their history one step will take them off my site. Obviously, this isn't what I want.

My guess is it would be a simple case of doing an if-statement but I'm not sure what or how to test for it. I suppose I could just test to make sure the base of the URL is my site but I'm not sure how.

Any tips or directions would be great.

A: 

No, it's not at simple as that. You can use the history collection to go back or forward, but you can't get the URLs in the history.

The only information that you can obtain about where the user comes from is the HTTP_REFERER string in the request header, but you have to use server side code to get that.

Guffa
You can also get the Referer header from document.referrer in JS. (Yes, despite the inconsistent spelling.)
bobince