jQueryを使って別タブで外部リンクを開くJavascript。 自分のドメインを指定して、それ以外のURLの記述があれば別タブで外部リンクを開くというものです。 もう一つポイントで、もしもaタグにtarget属性が含まれている外部ドメインなら、タブが二重で開いてしまう問題があるので、 もしtarget属性が含まれていたら先に属性をremoveしてあげる必要があります。 $(document).ready(function() { $('a').removeAttr('target'); $("a[href^=http://]").not($("a[href^=http://www.kaasan.info]")).click(function() { window.open($(this).attr("href")); return false; }); });
![jQueryでリンク先が外部ドメインだったらを別タブで開く](https://cdn-ak-scissors.b.st-hatena.com/image/square/acb33a7da915087515e048d3e742bed3a5e0ed93/height=288;version=1;width=512/http%3A%2F%2Fwww.kaasan.info%2Fwp-content%2Fuploads%2F2012%2F07%2FjQUery.gif)