モニターの中央にウインドウを開く
特定のclass名が設定されたa要素をクリックした時に新しいウインドウをモニターの中央に開く
<script type="text/javascript"> MyLib.event.observe(window, "load", function(){ openWindowCenter("open-center", 500, 334); }, false); </script> </head> <body> <div id="contents"> <a href="sub.html" class="open-center"><img src="images/image01.jpg" width="200" height="133" alt="image" /></a> <p>画像をクリックするとウインドウがモニターの中央に開きます。</p> </div> </body>
function openWindowCenter(tagetClassName, windowWidth, windowHeight){ //引数のいずれかに値が設定されていなければ処理を中断 if(!tagetClassName || !windowWidth || !windowHeight){ return false; } var anchor; var anchors = document.getElementsByTagName("a"); for(var i=0; anchor=anchors[i]; i++){ if(anchor.className.indexOf(tagetClassName) != -1){ //ウインドウを表示する座標を算出する MyLib.event.observe(anchor, "click", function(event){ //モニターサイズを取得 var screenWidth = window.screen.availWidth; var screenHeight = window.screen.availHeight; //モニターサイズの1/2の値から開くウインドウの大きさの1/2の値を引いてウインドウの座標を算出する var left = Math.round((screenWidth / 2) - (windowWidth / 2)); var top = Math.round((screenHeight / 2) - (windowHeight / 2)); window.open(this.href, "center", "width=" + windowWidth + ", height=" + windowHeight + ", top=" + top + ", left=" + left).focus(); MyLib.event.stop(event); }, false); } } }