[英]How to automatically open external links in new tab with PHP redirects?
目前,我们有一些通过我们的跟踪软件进行的php重定向,我想在访问者浏览器的新标签页中打开这些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>
我看到一些jQuery代码示例,这些示例根据域url自动识别外部链接,但是我们使用的是我们在域中托管的重定向,因此这些链接将被过滤掉。
与手动添加标签相比,这是可能的,但不一定有效。您将选择所有标签,并检查其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');
});
}
});
一种更有效的方法是调整选择器,使其仅选择以http://
开头的标签,但这仅在内部链接未以http://
开头的情况下才有效。
$("#content a[href^='http://']").attr("target","_blank");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.