- 2008-08-07 (木) 17:25
- jQuery
jQuery1.2.6用。別窓開くスクリプトの超簡易サンプル。
aタグを捜査してhrefに書かれたURLがhttpから始まってる場合のみtarget="_blank"を追加する。
JavaScript:
-
$("a").each(function (i) {
-
if ($(this).attr('href').indexOf("http") != -1) {
-
$(this).attr('target','_blank');
-
}
-
});
上のはIEでスルーされたのでwindow.openで分けてみる
JavaScript:
-
$("a").each(function (i) {
-
if ($(this).attr('href').indexOf("http") != -1) {
-
if(jQuery.browser.msie){
-
$(this).attr('onclick','window.open('this.href','status=yes,scrollbars=yes,directories=yes,menubar=yes,resizable=yes,toolbar=yes'); return false;');
-
}else{
-
$(this).attr('target','_blank');
-
}
-
}
-
});