javascript JS onclick="history.go(-1) 仅在我的域下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19417084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:30:36  来源:igfitidea点击:

JS onclick="history.go(-1) only if under my domain

javascriptphpjoomla

提问by ol30cean0

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:

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

<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?

如果访问者来自我的搜索页面或我网站的任何地方,点击这个他将被重定向到上一页,如果访问者来自我的网站之外,点击这个他将被重定向到我的主页?

回答by hexacyanide

You can use document.referrerand compare it to window.location.host.

您可以使用document.referrer它并将其与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:

所以你的 HTML 看起来像这样:

<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>

回答by Mike Edwards

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>

回答by Galili Omri

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://
  1. 删除 href=""
  2. “window.location.href”必须有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>