JS onclick="history.go(-1) 仅在我的域下 [英] JS onclick="history.go(-1) only if under my domain

查看:16
本文介绍了JS onclick="history.go(-1) 仅在我的域下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Joomla 网站,我有以下问题,我需要在我的产品详细信息页面上有一个返回搜索页面"功能,根据对我的初始问题的答复,我在进行了一些更改后使用了此代码:

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?

推荐答案

您可以使用 document.referrer 并将其与 window.location.host 进行比较.

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)

因此您的 HTML 将如下所示:

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>

这篇关于JS onclick="history.go(-1) 仅在我的域下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆