Forum Moderators: open
For instance, I am on a web page at widgets.com and I click a link that takes me to foo.com. Next I browse a few pages on foo.com. I would like a Javascript link on foo.com that takes me back to the last visited page of the referrer at widgets.com.
How would I do this? Is it possible to do this?
Thanks
Perhaps using a cookie value?
Anyhow, here is code for those who DON'T know how to send someone back a page (or two or three, simply replace -1 with desired value) :
<a href="javascript:history.go(-1);">insert code to link back</a>
good luck!
Patrick Elward
[edited by: crashomon at 9:44 pm (utc) on June 25, 2004]
I can't use a server side language.
I am using Mal's shopping cart and I am only allowed to use 255 characters of text and it can only be HTML or Javascript.
I want the visitor to be returned to the original product page when they click on the "continue shopping" button.
<script type="text/javascript">
var prevsite, mycookies = document.cookie;
var refpos = mycookies.indexOf("prevsite");
if (refpos == -1) {
// store the referrer
document.cookie = "prevsite=" + escape(document.referrer) + "path=/";
prevsite = document.referrer;
} else {
// read the value of prevsite in the cookie. 9 is the length of the string 'prevsite='
var endpos = mycookies.indexOf(";", refpos + 9);
if (endpos == -1) endpos = mycookies.length;
prevsite = mycookies.substring(refpos + 9, endpos);
prevsite = unescape(prevsite);
}
</script> The script looks whether a cookie called prevsite is set. If not, set it to the referrer property of the document object. Note that JavaScript knows how to spell referrer ;)
As you want the cookie to expire as soon as the session ends, there is no need to set a expiration date/time. You do want the cookie to be valid all over your site, so the path is set to the webroot, /.
The script also produces the variable prevsite, which contains the (stored) referrer. You can now easily make a link with something like
document.write('<a href="'+prevsite+'">previous site</a>');