2012-08-22 14 views
7

サイトのすべての外部リンクを新しいウィンドウで開こうとしています。しかし、そこに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のに新たなんです。

答えて

11

プログラムでクリックをトリガーするために、あなたのような何かを行うことができます。

$(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 
     } 
    }) 
}); 
+0

tech、返信いただきありがとうございます。申し訳ありませんが、私はページの上部にあるものとは何が違うのか分かりません。私は、私が外部ドメインとして働くとは思わないドメインをどこに追加するのか教えていただけますか? –

+0

回答を編集して除外リストのロジック(単純な解決策)の編集を参照してください。 – techfoobar

+0

元の答えは、どのようにプログラムのリンククリック(新しいタブで開くように)を起動することができるかを指摘するためのものでした。 – techfoobar

1

を、あなたは多分、クリックイベントのためのより良いフックを取得するためにHTMLを編集することができますか?内部または外部間の特定のリンクを分離する必要がある場合、HTML要素にrel値を適用します。あなたのJavaScriptで次に

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

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

EDIT:あなたはすでにどのようにこの程度のリンクのトンを、持っているように見..

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'); 
     }); 
     } 

    }); 
+0

返信いただきありがとうございます。はい、私はHTMLにアクセスできますが、何百ものリンクがあり、あなたはそれがしばらく時間がかかると想像することができます。しかし、私はあなたが将来のサイトビルドのためにそれをやったやり方を書き留めるつもりです。 –

0

は、私はこのようにそれを行うだろうと思います:

$(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のソリューションを使用してリンクのクリックをトリガーすることができます。それはあなたのウェブサイトのパフォーマンスに役立ちます。

+0

いいね、ありがとう。これは私のために働くように見えます。すぐに答えで更新されます。 –

0

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に行くすべてのリンクも既存のページで開くようになります。

0

あなたではなく、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; 
    }); 
0

これは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 
     } 
    }) 
}); 
を使用して、すべての外部ドメインのためのトリックを行います
+0

そのphpを 'location.hostname'で置き換えればPHPは必要なくなります – Uberfuzzy

関連する問題