IMUZA.com

<<WordPress(ConoHa)<<はてなブログ

ホーム / javascript / はてなブログのリンクを素の Javascript で別ウィンドウに開く(jQuery なし)

はてなブログのリンクを素の Javascript で別ウィンドウに開く(jQuery なし)

2016/07/14 javascript, はてなブログ

はてなブログのリンクは、標準ですと、外部リンクでも同じウィンドウで開きます。設定で変更することもできませんので、ググりますと、別ウィンドウで開くスクリプトがたくさんヒットします。

数ページ見ただけですが、素の Javascript で書かれたものがありませんでしたので書いてみました。

<script>
window.onload = function(){
var reg = new RegExp('^http(s)?://(?!' + location.hostname + ')');
Array.prototype.forEach.call(document.querySelectorAll('a'), function(node) {
if(reg.test(node.href)) node.setAttribute('target', '_blank');
});
};
</script>

(2016/9/6)querySelectorAll よりも getElementsByTagName の方が高速のようです。

<script>
window.onload = function(){
var reg = new RegExp('^http(s)?://(?!' + location.hostname + ')');
Array.prototype.forEach.call(document.getElementsByTagName('a'), function(node) {
if(reg.test(node.href)) node.setAttribute('target', '_blank');
});
};
</script>

設定 > 詳細設定 > headに要素を追加 に入れるか、デザイン > カスタマイズ で HTML がかけるところに入れれば、リンクのうち外部リンクのみ target=_blank が追加され、別ウィンドウ(タブ)で開くようになります。

VAIO VGN-NW71FB ハードディスク換装
Joomla! 3.6 正式リリース! アップデートしてねとサイトから自動メールがきたよ!
Twitter
Facebook
ブックマーク
LINEで送る

最初のサイドバー

最新記事

2022/06/26

WordPress:Aレコード変更でウェブだけ他サーバへ移転

2022/06/13

WordPress Popular Posts 他のプラグインに変えたほうがいいかも

2022/06/06

WordPress Popular Posts 表示されないを解決する

2022/06/03

WordPress Popular Posts 表示されない(解決だが…)

2022/05/29

WordPress:テーマをゼロから作ってみる(2)

最新記事を一覧で見る

よく読まれている記事

カテゴリー

  • はてなブログ211
  • WebTips108
  • javascript95
  • Joomla!88
  • Windows68
  • CSS62
  • Joomla!更新53
  • Linux49
  • はてなテーマ45
  • Wordpress34
  • Plamo33
  • Google32
  • はてなプラグイン25
  • php18
  • Node.js18
  • Ubuntu16
  • SASS16
  • laravel415
  • cms-style10
  • Chrome10
  • iPhone9
  • Git入門6
  • genesis6
  • Android4
  • Python4
  • ConoHa WING4
  • 静的サイトジェネレーター3
  • Firefox3
  • Blankslate3
  • Docker3
  • SSD3
  • Facebook3
  • スマートフォン3
  • GitHub2
  • Mactype2
  • 映画1
  • rails入門1
  • Twitter1
  • はてなブクマ1
  • youtube1

Footer

My Web Sites

  • @半径とことこ60分
  • そんなには褒めないよ。映画評
  • IMUZA.com
  • GitHub

Another Sites

  • WordPress公式
  • WordPress関数リファレンス
  • PHPマニュアル

Contact Us

  • お問い合わせフォーム
  • Twitter
  • Facebook
  • Feedly

Copyright © 2022 · IMUZA.com