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

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

问题描述

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

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 =&quot; history.go(-1)仅在我的域名下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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