外部リンクを新しいウインドウで開く
a要素のhref属性に設定されたURLを検索し、自サイトのドメインとは異なる他サイトのURLが設定されている場合に別ウインドウで開く。
MyLib.event.observe(window, "load", openWindow, false); function openWindow(){ //window.locationオブジェクトのhostプロパティから、現在アクセスしているホスト名を取得し //href属性を検索するための正規表現オブジェクトを生成する var siteURI = new RegExp("http(s)?://" + window.location.host + "/", "i"); var windowName = "external"; var anchor; //HTML内の全てのa要素を取り出す var anchors = document.getElementsByTagName("a"); //取得したa要素を1つずつ取り出す for(var i=0; anchor=anchors[i]; i++){ if(!anchor.href.match(siteURI)){ MyLib.event.observe(anchor, "click", function(event){ window.open(this.href, windowName).focus(); MyLib.event.stop(event); }, false); } } }