Js Onclick="history.go(-1) Only If Under My Domain
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 t
Solution 1:
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:
<ahref=""onclick="if (document.referrer.indexOf(window.location.host) !== -1) { history.go(-1); return false; } else { window.location.href = 'website.com'; }"><?phpecho JText::_('VOLTAR'); ?></a>
Solution 2:
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>functionbackClick() {
// above conditional goes here.returnfalse;
}
</script><br/><ahref=""onclick="backClick()"><?phpecho JText::_('VOLTAR'); ?></a>
Solution 3:
I have tried some of the codes above and I needed to make some changes to make it work for me.
- remove href=""
- "window.location.href" must have http://
functionbackClick() {
if (document.referrer.indexOf(window.location.host) !== -1) {
history.go(-1); returnfalse;
}
else { window.location.href = 'https://stackoverflow.com'; }
}
<aonclick="backClick()">go back</a>
Post a Comment for "Js Onclick="history.go(-1) Only If Under My Domain"