10

Is there anyway to grab the referring URL using javascript, lets say the reffering url is http://page.com/home?local=fr, then redirect a user to a new page with the same local as the reffering page (http://page.com/login?local=referring local)?

Pseudo code would be something like this:

var referringURL = document.referrer;
var local = referringURL.substring(referringURL.indexOf("?"), referringURL.length())
var newURL = "http://page.com/login" +local;
Send user to newURL

Thanks, -Pete

Petey B
  • 11,439
  • 25
  • 81
  • 101

1 Answers1

18
if (document.referrer != "") {
   var referringURL = document.referrer;
   var local = referringURL.substring(referringURL.indexOf("?"), referringURL.length);
   location.href = "http://page.com/login" + local; 
}
Josh
  • 6,256
  • 2
  • 37
  • 56
  • 1
    If you are going to have more than one URL param you should probably add a method to parse them out, and then just grab the "local" param from the result. Example: http://rockmanx.wordpress.com/2008/10/03/get-url-parameters-using-javascript/ – Chelsea Jul 08 '09 at 15:50
  • thanks, just a note: "referringURL.length()" should be "referringURL.length". My "pseudo" code was pretty close to the actual thing, was very surprised as i have never coded something in javascrpit. – Petey B Jul 08 '09 at 16:56