クロスブラウザ実現のための基本関数群

最終更新日: 2004.12.23

Window( Frame )の幅の取得 
function getWindowWidth(){ if(window.innerWidth) return window.innerWidth; // Mozilla, Opera, NN4 if(document.documentElement && document.documentElement.clientWidth){ // 以下 IE return document.documentElement.clientWidth; } else if(document.body && document.body.clientWidth){ return document.body.clientWidth; } return 0; }
Window( Frame )の高さの取得 
function getWindowHeight(){ if(window.innerHeight) return window.innerHeight; // Mozilla, Opera, NN4 if(document.documentElement && document.documentElement.clientHeight){ // 以下 IE return document.documentElement.clientHeight; } else if(document.body && document.body.clientHeight){ return document.body.clientHeight; } return 0; }
Window( Frame )のスクリーン上の位置(X座標)

function getWinXOffset(){ if(window.scrollX) return window.scrollX; // Moziila if(window.pageXOffset) return window.pageXOffset; // Opera, NN4 if(document.documentElement && document.documentElement.scrollLeft){ // 以下 IE return document.documentElement.scrollLeft; } else if(document.body && document.body.scrollLeft){ return document.body.scrollLeft; } return 0; }
Window( Frame )のスクリーン上の位置(Y座標)

function getWinYOffset(){ if(window.scrollY) return window.scrollY; // Mozilla if(window.pageYOffset) return window.pageYOffset; // Opera, NN4 if(document.documentElement && document.documentElement.scrollTop){ // 以下 IE return document.documentElement.scrollTop; } else if(document.body && document.body.scrollTop){ return document.body.scrollTop; } return 0; }
レイヤ名からレイヤオブジェクトを得る 
function getDivFromName(nm){ // IE5+, Mozilla, Opera if(document.getElementById) return document.getElementById(nm); if(document.all) return document.all(nm); // IE4 if(document.layers){ // NN4 var s=''; for(var i=1; i<arguments.length; i++) s+='document.layers.'+arguments[i]+'.'; return eval(s+'document.layers.'+nm); } return null; }
レイヤを生成する // Opera 7以上
_createLayerNo = 0; // レイア生成番号
function createLayer(left,top,width,height,parentDiv){ var div=null; if(document.layers){ // NN4 div=parentDiv?(new Layer(width,parentDiv)):(new Layer(width)); if(height>0) div.clip.height =height; div.moveTo(left,top); } if(document.body){ var pDiv,txt,divName='_js_layer_'+_createLayerNo; _createLayerNo++; pDiv=parentDiv?parentDiv:document.body; var h=(height>0)?(height+'px'):''; var w=(width>0)? (width+'px'):!document.getElementById?';width:1':''; txt='<div id="'+divName+ '" style="position:absolute; left:'+left+'px; top:'+top+ 'px;'+' width:'+w+'; height:'+h+'; visibility:hidden;"><\/div>'; if(document.body.insertAdjacentHTML){ // IE, Opera pDiv.insertAdjacentHTML('BeforeEnd',txt); div=document.getElementById? document.getElementById(divName):(document.all?document.all(divName):null); } else if(document.createElement && document.body.appendChild && document.body.style){ // Mozilla pDiv.appendChild(div=document.createElement('DIV')); div.id=divName; div.style.position = 'absolute'; if(w!='') div.style.width = w; if(h!='') div.style.height= h; div.style.left=left+'px'; div.style.top =top+'px'; div.style.visibility='hidden'; } else if(document.body.innerHTML){ // others pDiv.innerHTML+=txt; div=document.getElementById? document.getElementById(divName):(document.all?document.all(divName):null); } } return div; }
外部参照レイアを生成する // Opera 7以上
_createLayerNo = 0; // レイア生成番号
function createExLayer(url,left,top,width,height,parentDiv){ var div=null; if(document.layers){ // NN4 var div=parentDiv?(new Layer(width,parentDiv)):(new Layer(width)); if(height>0) div.clip.height =height; div.moveTo(left,top); div.load(url,width); } if(document.body){ var pDiv,txt,divName = '_js_layer_'+_createLayerNo; _createLayerNo++; pDiv=parentDiv?parentDiv:document.body; txt='<div id="'+divName +'" style="position:absolute;left:'+left+'px; top:'+top +'px; width:'+width+'px; height:'+((height>0)?height+'px':'') +'; visibility:hidden;">' +'<iframe src="'+url+'" name="'+divName+'_if" ' +'width="'+width+'" height="'+height +'" marginwidth="0" marginheight="0" ' +'scrolling="no" frameborder="0">' +'<\/iframe>' +'<\/div>'; if(document.body.insertAdjacentHTML){ // IE, Opera pDiv.insertAdjacentHTML('BeforeEnd',txt); div=document.getElementById? document.getElementById(divName):(document.all?document.all(divName):null); } else if(document.createElement && document.body.appendChild && document.body.style){ // Mozilla div=document.createElement('IFRAME'); div.id=divName; div.name=divName; div.style.position = 'absolute'; div.style.width = width+'px'; div.style.height= height+'px'; div.style.left=left+'px'; div.style.top =top+'px'; div.style.visibility='hidden'; div.src=url; if(div.setAttribute){ div.setAttribute('frameborder',0); div.setAttribute('scrolling','no'); } pDiv.appendChild(div); } else if(document.body.innerHTML) { // others pDiv.innerHTML+=txt; div=document.getElementById? document.getElementById(divName):(document.all?document.all(divName):null); } } return div; }
外部参照レイアに表示中のドキュメントサイズ(高さ)の取得 // Opera 7以上
function getExlHeight(exdiv){ if(document.layers) return exdiv.document.height; // NN4 if(exdiv.contentDocument && exdiv.contentDocument.body.offsetHeight) return exdiv.contentDocument.body.offsetHeight; // Mozilla // 以下、IE, Opera if(frames[exdiv.id+'_if']&& frames[exdiv.id+'_if'].document && frames[exdiv.id+'_if'].document.body.scrollHeight) return frames[exdiv.id+'_if'].document.body.scrollHeight; if(frames(exdiv.id+'_if')&& frames(exdiv.id+'_if').document && frames(exdiv.id+'_if').document.body.scrollHeight) return frames(exdiv.id+'_if').document.body.scrollHeight; return 0; }
外部参照レイア内のドキュメントの縦方向へのスクロール // Opera 7以上
function scrollExlVItTo(exdiv,y){ if(document.layers){ // NN4 var dy=y-exdiv.clip.top, ch=exdiv.clip.height; exdiv.top-=dy; exdiv.clip.top=y; exdiv.clip.height=ch; } else if(frames[exdiv.id] && frames[exdiv.id].scrollTo) // Mozilla frames[exdiv.id].scrollTo(0,y); // 以下、IE, Opera else if(frames[exdiv.id+'_if'] && frames[exdiv.id+'_if'].scrollTo) frames[exdiv.id+'_if'].scrollTo(0,y); else if(frames(exdiv.id+'_if') && frames(exdiv.id+'_if').scrollTo) frames(exdiv.id+'_if').scrollTo(0,y); return; }
レイア内の IMAGEオブジェクトの取得 
function getDivImage(div,imgName){ // IE5+, Mozilla, Opera if(document.getElementById) return document.getElementById(imgName); if(div.document.images[imgName]) return div.document.images[imgName]; // NN4 if(document.images(imgName)) return document.images(imgName); // IE4 return null; }
レイヤの位置の初期化 

/* 現在の NN6(Mozilla)にはバグがあり、X座標についてはうまく初期化できない
   事があります。(このバグは、NN6.1あたりでは修正されています。)*/
function initDivPos(div){ if(document.layers) return; // IE5+, Mozilla, Opera 7 if(typeof div.style.left!="undefined"&& typeof div.style.left=="string"){ div.style.left=div.offsetLeft+'px'; div.style.top =div.offsetTop +'px'; } else if(typeof div.style.pixelLeft!="undefined"){ // IE4, Opera 6 div.style.pixelLeft=div.offsetLeft; div.style.pixelTop =div.offsetTop; } }
レイヤの位置(X軸方向)の取得 

/* NN6+(Mozilla)の場合、バグがあるので、それを考慮する場合は
   div.offsetTop のかわりに parseInt(div.style.top)を使い
   紫色のコード部分を追加します。
   ただし、このバグは、NN6.1あたりでは修正されています。*/
function getDivLeft(div){ if(typeof window.crypto!="undefined" && typeof window.getComputedStyle!="undefined"){ // Mozilla return parseInt(div.style.left); } else // IE, (Mozilla), Opera, NN4 return document.layers?div.left:(div.offsetLeft||div.style.pixelLeft||0); }
レイヤの位置(Y軸方向)の取得 

/* getDivLeft関数と同様、紫色のコード部分を追加します。*/
function getDivTop(div){ if(typeof window.crypto!="undefined" && typeof window.getComputedStyle!="undefined"){ // Mozilla return parseInt(div.style.top); } else // IE, (Mozilla), Opera, NN4 return document.layers?div.top:(div.offsetTop||div.style.pixelTop||0); }
レイヤを絶対値で移動する 
function moveDivTo(div,left,top){ if(document.layers){ div.moveTo(left,top); return; } // NN4 // IE5+, Mozilla, Opera 7 if(typeof div.style.left!="undefined"&& typeof div.style.left=="string"){ div.style.left=left+'px'; div.style.top =top +'px'; } else if(typeof div.style.pixelLeft!="undefined"){ // IE4, Opera 6 div.style.pixelLeft=left; div.style.pixelTop =top; } }
レイヤを相対値で移動する 

/* 現在のNN6(Mozilla)にはバグがあり、div.offsetLeftやdiv.offsetTopの代わりに
parseInt(div.style.left)及び parseInt(div.style.top)を使う必要が あります。NN6のバグを考慮する場合は、紫色のコード部分を追加します。 ただし、このバグは、NN6.1あたりでは修正されています。*/

function moveDivBy(div,left,top){ if(document.layers){ div.moveBy(left,top); return; } // NN4 if(typeof window.crypto!="undefined" && typeof window.getComputedStyle!="undefined"){ // Mozilla div.style.left=(parseInt(div.style.left)+Math.round(left))+'px'; div.style.top =(parseInt(div.style.top) +Math.round(top) )+'px'; return; } // IE5+, (Mozilla), Opera if(typeof div.style.left!="undefined"&& typeof div.style.left=="string"){ div.style.left=(div.offsetLeft+Math.round(left))+'px'; div.style.top =(div.offsetTop +Math.round(top) )+'px'; return; } // IE4 if(typeof div.style.pixelLeft!="undefined"){ div.style.pixelLeft+=Math.round(left); div.style.pixelTop +=Math.round(top); return; } }
レイヤのサイズの初期化 
function initDivSize(div){ if(document.layers) return; // IE5+, Mozilla, Opera 7 if(typeof div.style.width!="undefined"&& typeof div.style.width=="string"){ div.style.width =div.offsetWidth +'px'; div.style.height=div.offsetHeight+'px'; } else if(typeof div.style.pixelWidth!="undefined"){ // IE4, Opera 6 div.style.pixelWidth =div.offsetWidth; div.style.pixelHeight=div.offsetHeight; } }
レイヤのサイズ(幅)の取得 
function getDivWidth (div){ return document.layers? div.clip.width:(div.offsetWidth||div.style.pixelWidth||0); }
レイヤのサイズ(高さ)の取得 
function getDivHeight(div){ return document.layers? div.clip.height:(div.offsetHeight||div.style.pixelHeight||0); }
レイヤのサイズを絶対値で設定する 
function resizeDivTo(div,width,height){ if(document.layers) { // NN4 div.resizeTo(width,height); } // IE5+, Mozilla, Opera 7 else if(typeof div.style.width!="undefined"&& typeof div.style.width=="string"){ div.style.width =width +'px'; div.style.height=height+'px'; } else if(typeof div.style.pixelWidth!="undefined"){ // IE4, Opera 6 div.style.pixelWidth = width; div.style.pixelHeight = height; } }
レイヤのサイズを相対置で設定する 
function resizeDivBy(div,width,height){ if(document.layers) { // NN4 div.resizeBy(width,height); } // IE5+, Mozilla, Opera 7 else if(typeof div.style.width!="undefined"&& typeof div.style.width=="string"){ div.style.width =(div.offsetWidth +Math.round(width))+'px'; div.style.height=(div.offsetHeight+Math.round(height))+'px'; } else if(typeof div.style.pixelWidth!="undefined"){ // IE4, Opera 6 div.style.pixelWidth +=Math.round(width); div.style.pixelHeight+=Math.round(height); } }
レイヤの表示/非表示の設定 
function setDivVisibility(div,visible){ (div.style||div).visibility=(visible)? ((window.opera && !document.documentElement)?'visible':'inherit'):'hidden'; }
レイヤの表示領域のクリッピング // Opera 7以上
function setDivClip(div,top,right,bottom,left){ if(document.layers){ // NN4 div.clip.top =top; div.clip.right=right; div.clip.bottom=bottom;div.clip.left =left; } else if(typeof div.style.clip!="undefined"){ // IE, Mozilla, Opera div.style.clip= 'rect('+top+'px '+right+'px '+bottom+'px '+left+'px)'; } }
レイヤに HTMLを書く // Opera 7以上
function writeDivHTML(div,html){ if(document.layers){ // NN4 div.document.open(); div.document.write(html); div.document.close(); } else if(typeof div.innerHTML!="undefined"){ // IE, Mozilla, Opera div.innerHTML=html; } }
レイヤの背景色を設定する 
function setDivBackgroundColor(div,color){ if(document.layers){ // NN4 div.bgColor=(color=='transparent')?null:color; } else if(typeof div.style.backgroundColor!="undefined"){ // IE, Mozilla, Opera div.style.backgroundColor=(color==null)?'transparent':color; } }
レイヤの背景画像を設定する 
function setDivBackgroundImage(div,url){ if(document.layers){ // NN4 div.background.src=url; } else if(typeof div.style.backgroundColor!="undefined"){ // IE, Mozilla, Opera div.style.backgroundImage='url('+url+')'; } }
レイヤの重なる順序の設定 
function setDivZIndex(div,order){ (div.style||div).zIndex=order; }
イベント発生時のドキュメント上の座標(X座標)

function getEventPageX(e){ if(!e) var e = window.event; if(window.opera){ // Opera return (document.documentElement?window.pageXOffset:0)+e.clientX; } else if(e.pageX) return e.pageX; // Mozilla, NN4, Safari else if(e.clientX){ // IE, others var sl=0; if(document.documentElement && document.documentElement.scrollLeft) sl=document.documentElement.scrollLeft; else if(document.body && document.body.scrollLeft) sl=document.body.scrollLeft; else if(window.scrollX||window.pageXOffset) sl=(window.scrollX||window.pageXOffset); return sl+e.clientX; } return 0; }
イベント発生時のドキュメント上の座標(Y座標)

function getEventPageY(e){ if(!e) var e = window.event; if(window.opera){ // Opera return (document.documentElement?window.pageYOffset:0)+e.clientY; } else if(e.pageY) return e.pageY; // Mozilla, NN4, Safari else if(e.clientY){ // IE, others var st=0; if(document.documentElement && document.documentElement.scrollTop) st=document.documentElement.scrollTop; else if(document.body && document.body.scrollTop) st=document.body.scrollTop; else if(window.scrollY||window.pageYOffset) st=(window.scrollY||window.pageYOffset); return st+e.clientY; } return 0; }

(注) 基本関数での位置やサイズ情報の単位は、すべて pixel です。
また、IE4と書いてあっても、IE5+以上のコード部分で動く場合もあります。


Copyright(C) 2003-2004 Yoochan.
All rights reserved.