JS: redirect with referrer domainname and tld

Had to write this script to redirect visitors of a global information page back to the subdomain of the site they came from.

Visitor came from a link on this page: http://www.domain1.tld/category/post
Comes on a different website: http://www.domain2.tld/page
After clicking the button the visitor is sent to: http://subdomain.domain1.tld/

1
2
3
4
5
6
7
8
9
10
var AAP = {}; // global object
var referringDoc = document.referrer;
var referringUrl  = referringDoc.split('/')[2]; // chop the url at each / and use the second chop 
if (referringDoc != "") {
	AAP.referringHost = referringUrl.replace(/^[^.]+\./g, ""); // strip everything before the dot
}
else {
	AAP.referringHost = "domain.tld";
}
document.write("<a href=\"http://subdomain." + AAP.referringHost +"\" target=\"_self\" class=\"btn\"><span>Nice button to click on</span></a>");

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.