Redirect a page according to the reference URL via JavaScript

advertisements

How can I, by using JavaScript, redirect a page based on document.referrer?

http://www.url.com/thisfolder/someotherfolder    <-- referer

http://www.newurl.com/thisfolder                 <-- new url

This is what I have tried:

<script type="text/javascript" charset="utf-8">
   if (document.referrer == '' ||
       document.referrer.indexOf('www.website.com/thisfolder/andanything-here') != 1) {
       top.location="http://www.newpage.com";
   }
</script>


<script>
var ref=document.referrer;
if (ref.indexOf('www.oldurl.com')>0){
  ref=ref.replace('http://www.oldurl.com/oldfolder/');
  window.location.href='http://www.newurl.com/'+ref;
}
</script>

The above code gets the referrer. It checks whether the old site is in use. If so, it strips the beginning and redirects to the new site. The code should be self-explanatory.

The code works if your old site has urls like: www.oldurl.com/subdirectory/page1.htm

And the new site has the same set of files but a different path, e.g.: www.newurl.com/page1.htm