Javascript获取浏览器窗口大小 责任编辑:浩瀚天涯 点击数:391 更新时间:2007-3-3 8:17:51 <script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-0660578626274101&dt=1178522590015&lmt=1178522590&format=336x280_as&output=html&channel=6939299730&url=http%3A%2F%2Fwww.zxbc.cn%2FArticleContent.asp%3FID%3D7291&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=000000&color_border=FFFFFF&ad_type=text_image&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Ftn%3Dmax2_cb%26ie%3Dgb2312%26bs%3Daccess%2B%25D0%25A1%25CA%25FD%26sr%3D%26z%3D%26cl%3D3%26f%3D8%26wd%3Ddocument.body.clientWidth%253Fdocument.body.clientWidth%253Adocument.documentElement.clientWidth%26ct%3D0&cc=237&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> 在AJAX的运用中,我们可能会经常遇到这样的情况。要动态的创建以个层,而且必须在浏览器的最中央。不管浏览器是否是最大化。这时候我们就必须用到windows和document的几个属性。以下的函数将返回一个包含浏览器的大小和滚动条位置的一个数组。 var getWindowInfo=function(){var scrollX=0,scrollY=0,width=0,height=0,contentWidth=0,contentHeight=0;if(typeof(window.pageXOffset)=='number'){scrollX=window.pageXOffset;scrollY=window.pageYOffset;}else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){scrollX=document.body.scrollLeft;scrollY=document.body.scrollTop;}else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){scrollX=document.documentElement.scrollLeft;scrollY=document.documentElement.scrollTop;}if(typeof(window.innerWidth)=='number'){width=window.innerWidth;height=window.innerHeight;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){width=document.documentElement.clientWidth;height=document.documentElement.clientHeight;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){width=document.body.clientWidth;height=document.body.clientHeight;}if(document.documentElement&&(document.documentElement.scrollHeight||document.documentElement.offsetHeight)){if(document.documentElement.scrollHeight>document.documentElement.offsetHeight){contentWidth=document.documentElement.scrollWidth;contentHeight=document.documentElement.scrollHeight;}else{contentWidth=document.documentElement.offsetWidth;contentHeight=document.documentElement.offsetHeight;}}else if(document.body&&(document.body.scrollHeight||document.body.offsetHeight)){if(document.body.scrollHeight>document.body.offsetHeight){contentWidth=document.body.scrollWidth;contentHeight=document.body.scrollHeight;}else{contentWidth=document.body.offsetWidth;contentHeight=document.body.offsetHeight;}}else{contentWidth=width; contentHeight=height;}if(height>contentHeight)height=contentHeight;if(width>contentWidth)width=contentWidth;var rect=new Object();rect.ScrollX=scrollX;rect.ScrollY=scrollY;rect.Width=width;rect.Height=height;rect.ContentWidth=contentWidth;rect.ContentHeight=contentHeight;return rect;}