0

I'm trying to add an on-page "back" link that will only appear if the referrer was the home domain.

I have been through a number of threads on here which have been very helpful, but my code does not work despite being a combination of others' supposedly working code. I am at a loss as to why:

if (document.referrer.indexOf('MyDomain') > -1) {
    document.write('<a href="' + document.referrer + '">&lt;&lt;Back to Results</a>');
}

When the referrer is on my domain it works as intended, but when the referrer is not on my domain it still writes the link.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • 2
    What is the value of `document.referrer`? – Quentin Mar 03 '16 at 17:09
  • I see you're a new member of Stack Overflow, please be aware of just how active SO is - when you ask a question it's worth hanging around to see if there are any follow up comments such as Quentin's above, as a question will typically have a fair amount of views almost immediately. – James Thorpe Mar 03 '16 at 17:18
  • Thanks for your advice re regular checking. – David F Mar 08 '16 at 14:25
  • In answer to Quentin's question, the value of document.referrer; – David F Mar 08 '16 at 14:25
  • In answer to Quentin's question, the value of document.referrer; If the previous page is on my domain: http://**mydomain.co/apage.aspx if coming from a different domain: http://**www.otherdomain.co/apage.html Without the asterisks of course. Is your point that the target string would need to be at the start of the searched string? i.e. that i need to avoid the HTTP? – David F Mar 08 '16 at 14:32

0 Answers0