Last active
July 31, 2022 05:45
-
-
Save okuden-labo/a53927e51187d3951ead to your computer and use it in GitHub Desktop.
jQueryで外部リンクを別タブで開く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http:で始まるリンク先に、自動的に target="_blank" を追加します。 | |
$(function(){ | |
$("a[href^='http://']").attr("target","_blank"); | |
}); | |
//自サイトのホスト名を含まないリンクを対象にする場合 | |
$(function() { | |
$("a[href^=http]:not([href*='" + location.hostname + "'])" | |
).attr("target", "_blank"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment