AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Back Button

Back Button



The code below will allow you to put an intelligent back link on to your pages. When used sparingly it can add more value to your site than using the history property.

<script language="JavaScript" type="text/javascript">
<!--
function checkurl()
{
   var myurl = "http://www.yourwebsite.com";
   if(document.referrer.substring(0,26) == myurl)
    {  document.location.href=document.referrer;  }
  else
    {  document.location.href=myurl;  }
}
// -->
</script>

Normally using the history.go(-1) property takes you back to the page you came from - which could be from another site. This would mean your back link could be redirecting users away from your site.

This small script will only go back to the page you came from IF that page is on your site. If the page is from another url or from an email link - the back link will take you to your homepage which means you get to keep your visitor a while longer :)

Click here to see the page you came from.

There are only a couple of changes you will need to make for it to work on your pages

  • Change the value of myurl to your website address
  • Change the value of document.referrer.substring(0,26) to the length of the myurl string

Finally, to create the link add the following code into your document.

<a href=" <!-- insert url --> " onclick="checkurl();return false;">back</a>

Replace <!-- insert url --> with the url of your homepages. This means that non Javascript browsers will be sent to your homepage if they click on the back link.








Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]