頁面需要嵌套在跨域的iframe中,而頁面高度不固定,需要每個頁面把自己的高度獲得后,通過js通知iframe調(diào)整顯示。
而頁面在獲得自己的高度時(shí),發(fā)現(xiàn)總是比預(yù)想的大。經(jīng)過參考別人的博客,發(fā)現(xiàn)原來是w3c標(biāo)準(zhǔn)的原因。
也就是要在<html>前面加上<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
加上之后,document.documentElement.scrollHeight在IE和Chrome下,可以正常取到合適的全文高度,但是firefox下取到的則過高;用document.body.scrollHeight取則恰好相反。
因此最后采用了Math.min(document.documentElement.scrollHeight,document.body.scrollHeight)獲得最小高度,正常。
但是加上W3C標(biāo)準(zhǔn)后,畫面也出現(xiàn)了一些小的不同,比如頁面的行間距,似乎IE的就要短一些,有些地方重合了。而且css也會嚴(yán)格區(qū)分大小寫。
參考文章url:
http://www.survivalescaperooms.com/article/19844.htm
http://www.survivalescaperooms.com/article/32874.htm
內(nèi)容:
js獲取頁面高度 2011-06-02 14:20:02| 分類:JavaScript | 標(biāo)簽:body document clientwidth 寬度 頁面高度 |字號大中小 訂閱.
<script>
function getInfo()
{
var s = “”;
s += ” 網(wǎng)頁可見區(qū)域?qū)挘?#8221;+ document.body.clientWidth;
s += ” 網(wǎng)頁可見區(qū)域高:”+ document.body.clientHeight;
s += ” 網(wǎng)頁可見區(qū)域?qū)挘?#8221;+ document.body.offsetWidth + ” (包括邊線和滾動條的寬)”;
s += ” 網(wǎng)頁可見區(qū)域高:”+ document.body.offsetHeight + ” (包括邊線的寬)”;
s += ” 網(wǎng)頁正文全文寬:”+ document.body.scrollWidth;
s += ” 網(wǎng)頁正文全文高:”+ document.body.scrollHeight;
s += ” 網(wǎng)頁被卷去的高(ff):”+ document.body.scrollTop;
s += ” 網(wǎng)頁被卷去的高(ie):”+ document.documentElement.scrollTop;
s += ” 網(wǎng)頁被卷去的左:”+ document.body.scrollLeft;
s += ” 網(wǎng)頁正文部分上:”+ window.screenTop;
s += ” 網(wǎng)頁正文部分左:”+ window.screenLeft;
s += ” 屏幕分辨率的高:”+ window.screen.height;
s += ” 屏幕分辨率的寬:”+ window.screen.width;
s += ” 屏幕可用工作區(qū)高度:”+ window.screen.availHeight;
s += ” 屏幕可用工作區(qū)寬度:”+ window.screen.availWidth;
s += ” 你的屏幕設(shè)置是 “+ window.screen.colorDepth +” 位彩色”;
s += ” 你的屏幕設(shè)置 “+ window.screen.deviceXDPI +” 像素/英寸”;
//alert (s);
}
getInfo();
< /script>
在我本地測試當(dāng)中:
在IE、FireFox、Opera下都可以使用
document.body.clientWidth
新聞熱點(diǎn)
疑難解答
圖片精選