tags:

views:

32

answers:

2

Let's say I have a page, http://mydomain.com/mypage.html, with a "Back to page" link. This link should take the user back to the page where they came from only if the previous page URL matches one of the following: http://mydomain.com/one.html, http://mydomain.com/two.html, and http://mydomain.com/three.html. Otherwise, it would take the user back to the homepage, http://mydomain.com. I would like the "Back to page" link to also take the user back to the homepage when http://mydomain.com/mypage.html is pasted onto the browser. How can i accomplish this with Javascript. Thanks!

+1  A: 

I think it's a mistake to try to do this only with client-side code. First, there's no reliable way to tell where you came from. To be reliable, your pages should pass through the source page when they link to "mypage.html". That way the server can drop the origin page into some hidden field, or into a Javascript variable, or whatever, and your code can be sure that however weird the user's browser might be, it has a good value to work with.

Pointy
A: 
if(document.referrer  == 'http://mydomain.com/one.html' || document.referrer == 'http://mydomain.com/two.html') {
     window.location = document.referrer;
} else {
    window.location = 'http://mydomain.com/';
}

and

if(window.location  == 'http://mydomain.com/mypage.html') {
    window.location = 'http://mydomain.com/';
}

but as others have said, performing redirects using a client-side method is not a great way of doing things.

MatW