Dom 如何获得HTA窗口的大小?
您可以设置HTA窗口的大小,但我找不到获得其大小的方法 我所能想到的就是阅读Dom 如何获得HTA窗口的大小?,dom,hta,windows-scripting,Dom,Hta,Windows Scripting,您可以设置HTA窗口的大小,但我找不到获得其大小的方法 我所能想到的就是阅读document.body.offsetWidth和offsetHeight,但它们给出的是视口大小,而不是实际的窗口大小 有可能知道吗?似乎没有属性或方法来获取这些信息。现在发布的测试版IE9有了新的属性outterWidth和outterHeight,但这不是现在的选项 所以我设计了一个解决方案: function addWinSizeGetterFuncs(){ var currViewportWidth =
document.body.offsetWidth
和offsetHeight
,但它们给出的是视口大小,而不是实际的窗口大小
有可能知道吗?似乎没有属性或方法来获取这些信息。现在发布的测试版IE9有了新的属性
outterWidth
和outterHeight
,但这不是现在的选项
所以我设计了一个解决方案:
function addWinSizeGetterFuncs(){
var currViewportWidth = document.body.offsetWidth ;
var currViewportHeight = document.body.offsetHeight ;
resizeTo(currViewportWidth,currViewportHeight) ;
var chromeWidth = currViewportWidth-document.body.offsetWidth ;
var chromeHeight = currViewportHeight-document.body.offsetHeight ;
resizeTo(currViewportWidth+chromeWidth,currViewportHeight+chromeHeight) ;
window.getWidth = new Function('return document.body.offsetWidth+'+chromeWidth) ;
window.getHeight = new Function('return document.body.offsetHeight+'+chromeHeight) ;
}
该函数的作用是为window
对象创建两个新方法:window.getWidth()
和window.getHeight()
。它们将获得窗口的实际大小。该函数要求存在
body
对象,因此必须在body
标记之后执行
现在我们已经做到了,同样的问题也适用于窗口位置。您无法获取实际的窗口位置。可以得到的是相对于屏幕的视口位置。 因此,同样的解决方法也适用于此:
function addWinPosGetterFuncs(){
var currViewportLeft = screenLeft ;
var currViewportTop = screenTop ;
moveTo(currViewportLeft,currViewportTop) ;
var viewportOffsetX = screenLeft-currViewportLeft ;
var viewportOffsetY = screenTop-currViewportTop ;
moveTo(currViewportLeft-viewportOffsetX,currViewportTop-viewportOffsetY) ;
window.getScreenX = new Function('return screenLeft-'+viewportOffsetX) ;
window.getScreenY = new Function('return screenTop-'+viewportOffsetY) ;
}
同样,该函数创建两个新方法来检索实际窗口位置:window.getScreenX()
和window.getScreenY()
问题解决了。我会让你们知道函数是如何工作的;) 缩小版:
window.pos=new function(w,h,x,y){with(document.body)return(
resizeTo(w=offsetWidth,h=offsetHeight),resizeTo(w+(w=w-offsetWidth),h+(h=h-offsetHeight)),
moveTo (x=screenLeft ,y=screenTop ),moveTo (x-(x=screenLeft-x ),y-(y=screenTop-y )),{
w:function(){return offsetWidth +w},
h:function(){return offsetHeight+h},
x:function(){return screenLeft -x},
y:function(){return screenTop -y}
})};