如何使用PHP重定向自动打开新选项卡中的外部链接?

pap*_*lon 2 php jquery redirect external hyperlink

我们目前有一些php重定向通过我们的跟踪软件,我想在访问者浏览器的新标签中打开,但不想通过手动添加target="_blank"或浏览整个网站rel=external.

我们目前的链接看起来像这样

<a href="http://www.website-shown-browser.com" onclick="this.href='http://'+window.location.host+'/visit/redirect.php'">Website</a>
Run Code Online (Sandbox Code Playgroud)

我看到一些jQuery代码示例,它们根据域URL自动识别外部链接,但是我们正在使用我们在域上托管的这些重定向,因此这些链接将被过滤掉.

Aus*_*tin 5

这是可能的,但不一定有效,而不是手动添加.您将选择所有标签并检查它们的href是否是外部的.然后你可以为这个元素设置属性target,_blank如果它是:

$('a').each(function() {
   var a = new RegExp('/' + window.location.host + '/');
   if(!a.test(this.href)) {
       $(this).click(function(event) {
           event.preventDefault();
           event.stopPropagation();
           window.open(this.href, '_blank');
       });
   }
});
Run Code Online (Sandbox Code Playgroud)

一种更有效的方法是调整选择器以仅选择以...开头的标签http://,但这只有在内部链接没有前缀的情况下才有效http://:

$("#content a[href^='http://']").attr("target","_blank");
Run Code Online (Sandbox Code Playgroud)