12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

違うドメインにリンクしている場合target="_blank"を付与するスクリプト

12
Last updated at Posted at 2015-08-13

毎回書くのでメモ。
ウッカリ忘れも防止できるので、このぐらいの処理はクライアントサイドでやるのがいいんじゃないかなって思ってる。

// コンテンツラッパー内のaタグを走査する
$('body').find('a').each(function(){
  var attr = $(this).attr('href');
  // アンカーリンクではない、かつ、自ドメインのリンクではないという条件を判定
  if(attr.search(/^#/) !== 0 && attr.search(/\//) !== 0 && !attr.match(YOUR_DOMAIN)) {
    $(this).attr('target','_blank');
  }
});
12
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?