21

I have a Joomla site, and I have the following problem, I need a "Back to search page" function on my product details page, and I am using this code after some changes according to replies to my initian question:

<br/><a href="" onclick="if (document.referrer.indexOf(window.location.host) !== -1) alert('true'); { history.go(-1); return false; } else { window.location.href = 'mysite.com.br'; }"><?php echo JText::_('VOLTAR'); ?></a>

Now, if a Visitor comes from another site directly to my product page and click this, he will be redirected to homepage of my site, and this is ok, but if I in a search page of my site, click on a product page and then click on the back to search link, the visitor is also redirected to my homepage, which is not good, it should be redirected to previous page, which was his own search page.

Is there any way to modify this code in order to accomplish something like:

if visitor comes from my search page or from anywhere in my site, by clicking this he will be redirected to the previous page, and if a visitor came from outside of my site, by clicking this he will be redirected to my homepage?

ol30cean0
  • 481
  • 4
  • 8
  • 18
  • This should definitely be combined with http://stackoverflow.com/a/24056766/37055 since even if a user is on your site there's many reasons they won't have a history back (new window, https to http, etc) – Chris Marisic Apr 12 '16 at 14:12

3 Answers3

27

You can use document.referrer and compare it to window.location.host.

if (document.referrer.split('/')[2] === window.location.host)
if (document.referrer.indexOf(window.location.host) !== -1)

So your HTML will look like this:

<a href="" onclick="if (document.referrer.indexOf(window.location.host) !== -1) { history.go(-1); return false; } else { window.location.href = 'website.com'; }"><?php echo JText::_('VOLTAR'); ?></a>
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • yes, that is cool, but I cant manage to combine the onclick argument with HTML, JS and PHP in order to have a hyperlink text in the page "Back to search": `
    `, can you help?
    – ol30cean0 Oct 17 '13 at 02:52
  • Modified answer. Change `website.com` to whatever website you want to redirect to, or use `window.location.host` to use the current URL without a path. – hexacyanide Oct 17 '13 at 02:57
  • I tested it in Firefox and Chrome, I came to my page from a google link, I clicked the back button and even if i can see in the bottom bar of the browser before clicking the link that the link of the button seems to point to the homepage of my site, if I click it, I am redirected back to google, weird... can you fix it? – ol30cean0 Oct 17 '13 at 03:25
  • What was `document.referrer` at the time of redirection? – hexacyanide Oct 17 '13 at 03:27
  • sorry, I didnt understand your question, I pasted your code and changed website.com with my websiteURL, the referrer was a google page, but I think I did not understand your question, sorry – ol30cean0 Oct 17 '13 at 04:58
  • Do `if (document.referrer.indexOf(window.location.host) !== -1) alert('true');` for the `onclick` handler and tell me if it alerts the message. Also, please edit your question with your current code. – hexacyanide Oct 17 '13 at 05:00
  • hello, I updated the code in my question and the question itself, with your actual code if I come to my site from google and click back to search i am redirected to my website homepage and this is good, but if the visitor is inside a search page of my site and click on a product page and then clicks on the back to search link, he is also redirected to the homepage of my site and not to the previous page – ol30cean0 Oct 17 '13 at 16:30
  • What are the URLs of those pages? – hexacyanide Oct 17 '13 at 22:30
  • sorry but I dont think that posting URLs here could help with this, I prefer not to reveal real UrLs, is that a problem for you? Do you have an idea of the reason why this sint working? thanks! – ol30cean0 Oct 17 '13 at 23:17
  • From what you have provided, it is not possible to find a reason if you cannot debug it yourself. – hexacyanide Oct 17 '13 at 23:19
  • do you think this could help me? [link]http://stackoverflow.com/questions/4362761/how-can-i-determine-if-the-document-referrer-is-from-my-own-site[/link] – ol30cean0 Oct 18 '13 at 03:39
  • Why don't you give it a try instead of asking me? – hexacyanide Oct 18 '13 at 03:40
  • because I am not capable of using that syntax my friend, i am NO PROGRAMMER, I just like these things, but I have no knowledge in order to use that syntax on my case... and well, I am asking you becaus eyou were helping me, in this site many times people are not kind, you are helping me and you feel surprised bcs I asked you about that thread? well, no problem, if you can help me thanks, if not, no problem. – ol30cean0 Oct 19 '13 at 15:10
  • @hexacyanide note that `website.com` is interpreted as a relative url, hence will not work as expected. Using `http://website.com` fixes this issue. – Adriano Jun 20 '14 at 08:29
  • @hexacyanide using `replace("http://website.com")` may be a better solution: "`replace()` does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco", see http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Adriano Jun 20 '14 at 08:31
  • 2
    does not work if I come from a page where I forced to open the page in a new tab. – Ka. Jun 02 '20 at 16:13
7

Adding branching logic into an inline click handler gets messy. If you can move this to a function and reference it it will be far more readable.

if(document.referrer.indexOf('mysite.com') >= 0) {
    history.go(-1);
}
else {
    window.location.href = 'myHomePageUrl'; // this might just be '/' of your site
}

Edit: If you are not concerned about adding names to the pages global scope you can create a function in a script tag immediately before the link you are creating:

<script>
    function backClick() {
        // above conditional goes here.
        return false;
    }
</script>
<br/><a href="" onclick="backClick()"><?php echo JText::_('VOLTAR'); ?></a>
Mike Edwards
  • 3,742
  • 17
  • 23
  • nice, and so how can I combine the onclick argument with HTML, JS and PHP? sorry I am a bit confused, I need to combine this IF clause with the following code in order to have a text in the page saying like "Back to search": `
    `, I am no programmer, but i am trying here to solve this, can you help?
    – ol30cean0 Oct 17 '13 at 02:50
  • See my edit above. As your page becomes more complex you'll eventually want to move most of your Javascript to a separate script file and register your event handlers from within closures to avoid leaking names into the global namespace. That starts to get pretty advanced, though, so if your page is simple this should do the trick. – Mike Edwards Oct 17 '13 at 16:13
  • hello, your script is doing the opposite that I need, I mean that if the visitor comes from other domain, the script calls the `history.go (-1)` and the visitor is sent back to the other domain, like to google, and if the visitor comes to a product page from a search page of my site, and I click in the back to search link, he is redirected to homepage of my site, can you fix it? – ol30cean0 Oct 17 '13 at 16:41
  • Just to confirm, you are using the code above exactly as it appears now, but replacing "mysite.com" with your domain name? I did edit the post yesterday shortly after originally posted because there was an error in the condition, I just want to make sure you have: ...indexOf('mysite.com') _>= 0_ – Mike Edwards Oct 17 '13 at 16:47
  • yes, I am using the code just replacing mysite.com with my own domain URL, I am also replacing myHomePageUrl with my homepage url without http://, yes, and also I am using >=0, yes, so what could be the problem? – ol30cean0 Oct 17 '13 at 16:58
  • hello my friend, can you help me to debug this? – ol30cean0 Oct 19 '13 at 15:10
4

I have tried some of the codes above and I needed to make some changes to make it work for me.

  1. remove href=""
  2. "window.location.href" must have http://

function backClick() {
   if (document.referrer.indexOf(window.location.host) !== -1) {
    history.go(-1); return false;
   }
   else { window.location.href = 'https://stackoverflow.com'; }
    }
<a onclick="backClick()">go back</a>
Vencovsky
  • 28,550
  • 17
  • 109
  • 176
Galili Omri
  • 310
  • 5
  • 17