从一个域打开所有的外部链接打开一个新标签分开(Open all external links ope

2019-07-31 03:34发布

我试图打开该网站上的所有外部链接在新窗口中。 然而在有2个版本的网站。 例如,存储和主站点。 等主要网站,我们可能有去链接http://store.site.com的例子。

我有一些代码在这里,这将让我打开一个新的窗口中的所有外部链接。 不过,我想能够排除某些域。 喜欢的人,我上面提到的。

下面是代码:

$(document).ready(function() {
   $("a[href^=http]").each(function(){
      if(this.href.indexOf(location.hostname) == -1) {
         $(this).attr({
            target: "_blank",
            title: "Opens in a new window"
         });
      }
   })
});

我是新来的JS / jQuery的使尽可能多的信息将是辉煌的。

Answer 1:

对于编程触发点击,你可以这样做:

$(document).ready(function() {

   $("a[href^=http]").each(function(){

      // NEW - excluded domains list
      var excludes = [
         'excludeddomain1.com',
         'excludeddomain2.com',
         'excluded.subdomain.com'
      ];
      for(i=0; i<excludes.length; i++) {
         if(this.href.indexOf(excludes[i]) != -1) {
            return true; // continue each() with next link
         }
      }

      if(this.href.indexOf(location.hostname) == -1) {

           // attach a do-nothing event handler to ensure we can 'trigger' a click on this link
           $(this).click(function() { return true; }); 

           $(this).attr({
               target: "_blank",
               title: "Opens in a new window"
           });

           $(this).click(); // trigger it
      }
   })
});


Answer 2:

您可以编辑HTML,以获得更好的挂钩,也许点击事件? 如果我需要分开内部或外部之间的某些联系我将应用HTML元件上的相对值。

    <a href="URL" rel="external">Link</a>

然后,在JavaScript

    $('a[rel="external"]').click( function(event) {
     event.stopPropagation();
     window.open( $(this).attr('href') );
     return false;
    });

编辑:看到,因为你已经有一吨的链接,这个怎么样..

    var a = new RegExp('http:\/\/store.blah.com');

    $('a').each(function() {

      if(a.test(this.href)) {
        $(this).click(function(event) {
         event.preventDefault();
         event.stopPropagation();
         window.open(this.href, '_blank');
        });
      }

    });


Answer 3:

我想我会做这样的:

    $(document).ready(function() {
      $("a[href^=http]").each(function(){
         if(this.href.indexOf(location.hostname) == -1 && this.href.indexOf("store.domain.com") == -1 && this.href.indexOf("other.domain.rule") == -1) {
            $(this).attr({
               target: "_blank",
               title: "Opens in a new window"
           });
         }
       })
    });

它有点手册,但,如果你不想处理拆分字符串和数组,这是解决方案。 我敢肯定,这会有所帮助。

编辑:而除了这个,你可以使用techfoobar的解决方案,触发链接的点击次数。 这将帮助你和你的网站的性能。



Answer 4:

沿着相同的路线作为techfoobar的回答,您可以建立应留在同一个窗口中打开域的列表。 您可以使用,虽然经常expresions更健壮的方式做到这一点。 如果你只是做一个直的indexOf()来检查你将跳过具有匹配的子域,但不匹配域名的链接,虽然你可以离开了“$”,如果你想在href串之间是否匹配的名称。

此实现应该做你想要什么,并有少量修改,以你所需要的代码。

$(document).ready(function() {
    //populate this list with whatever domain names you want, the 
    //$ sign matches the end of the string, only top level domains are affected
    var whiteList = [/google.com\/$/, /stackoverflow.com\/$/];

   $("a[href^=http]").each(function(){
      if(this.href.indexOf(location.hostname) == -1) {

        //check if the href of the current link matches any of our patterns
        var href = this.href;
        if(whiteList.filter(function(x){return x.test(href)}).length == 0) {

         $(this).attr({
            target: "_blank",
            title: "Opens in a new window"
         });
        }
      }
   })
});

有了这个例子中,所有的链接去到google.com和stackoverflow.com将留在现有页面以及打开。



Answer 5:

如果你想用一个事件处理程序的身体比改变DOM,我建议这样的事情...

  // open external links in a new tab
  $('body').on('click','a',function(){
    var $a = $(this);
    var href = $a.attr('href');
    if (href.indexOf('/') == 0) return;  // ignore relative links
    var target = $a.attr('target') || "";
    if (target.length > 0) return; // ignore links with a target attribute already
    window.open(href, '_blank');  // open external links in a new tab
    return false;
  });


Answer 6:

这将这样的伎俩,使用PHP所有外部域

$(document).ready(function() {
   $("a[href^=http]").each(function(){

      // NEW - excluded domains list
      var excludes = [
         '<?php echo $_SERVER['HTTP_HOST']; ?>'
      ];
      for(i=0; i<excludes.length; i++) {
         if(this.href.indexOf(excludes[i]) != -1) {
            return true; // continue each() with next link
         }
      }

      if(this.href.indexOf(location.hostname) == -1) {

           // attach a do-nothing event handler to ensure we can 'trigger' a click on this link
           $(this).click(function() { return true; }); 

           $(this).attr({
               target: "_blank",
               title: "Opens in a new window"
           });

           $(this).click(); // trigger it
      }
   })
});


Answer 7:

如果你只是想所有链接不符合你的域名:

var all_links = document.querySelectorAll('a');
for (var i = 0; i < all_links.length; i++){
       var a = all_links[i];
       if(a.hostname != location.hostname) {
               a.rel = 'noopener';
               a.target = '_blank';
       }
}


文章来源: Open all external links open in a new tab apart from a domain