http://phpjs.org/functions/preg_quote:491
http://www.tohoho-web.com/js/regexp.htm
http://semooh.jp/jquery/
</head>よりも前に記述します。
typeofの文字列判定がダメかもしれません。
target指定がある。 => そのまま http://などから書いていない。 => _top http(s)://自ドメイン/... => _top それ以外 => _blank javascriptがoff => 未指定時を_top(baseタグ) jquery未対応かコードに問題 => 毎回javascriptがエラーになるなど。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script language="JavaScript">
//<![CDATA[<!--
try{(function(){
$(document).ready(function(){
var hostnamePattern = new RegExp(
"^https?://"
+ "(?:[^/]+\\.)?"
+ location.hostname.replace(new RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]", "g"), "\\$&")
+ "/"
),
protocolPattern = new RegExp("^[A-Za-z]+://");
$("a").each(function(i){
if (typeof($(this).attr("href")) == "string" && typeof($(this).attr("target")) != "string")
{
if (!$(this).attr("href").match(protocolPattern) || $(this).attr("href").match(hostnamePattern))
{
$(this).attr("target", "_top");
}
else
{
$(this).attr("target", "_blank");
}
}
});
});
})();}catch(e){}
//-->]]>
</script>
<base target="_top">
2011-11-26 追記
_topはブラウザによってはnocacheなので_selfが良いです。
下記の例は$(document).ready(function(){...});の中か、</body>直前に書きます。
(function(){
var hostnamePattern = new RegExp(
"^https?://"
+ "(?:[^/]+\\.)?"
+ location.hostname.replace(new RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]", "g"), "\\$&")
+ "/"
),
protocolPattern = new RegExp("^[A-Za-z]+://");
$("a").each(function(){
var elem = $(this);
if (typeof elem.attr("href") == "string" && typeof elem.attr("target") != "string")
{
if (!elem.attr("href").match(protocolPattern) || elem.attr("href").match(hostnamePattern))
{
elem.attr("target", "_self");
}
else
{
elem.attr("target", "_blank");
}
}
});
})();
0 件のコメント:
コメントを投稿