js中的文档模式-document.compatMode
来源:WEB前端开发 时间:2009-08-14 20:30:00
今天在看框架的时候无意间看到了document.compatMode,经过一番资料查找,终于搞懂了。文档模式在开发中貌似很少用到,最常见的是就是在获取页面宽高的时候,比如文档宽高,可见区域宽高等。
IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有很大差别,而在不声明Doctype的情况下,IE默认又是Quirks Mode。所以为兼容性考虑,我们可能需要获取当前的文档渲染方式。
document.compatMode正好派上用场,它有两种可能的返回值:BackCompat和CSS1Compat。
BackCompat:标准兼容模式关闭。浏览器客户区宽度是document.body.clientWidth;CSS1Compat:标准兼容模式开启。 浏览器客户区宽度是document.documentElement.clientWidth。
那么写了个准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top的代码:
if (document.compatMode == "BackCompat") {
cWidth = document.body.clientWidth;
cHeight = document.body.clientHeight;
sWidth = document.body.scrollWidth;
sHeight = document.body.scrollHeight;
sLeft = document.body.scrollLeft;
sTop = document.body.scrollTop;
}
else { //document.compatMode == "CSS1Compat"
cWidth = document.documentElement.clientWidth;
cHeight = document.documentElement.clientHeight;
sWidth = document.documentElement.scrollWidth;
sHeight = document.documentElement.scrollHeight;
sLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft;
sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用CSS实现柱状图(Bar Graph)的方法(三)——复杂柱状图的实现
![](https://img.aspxhome.com/file/UploadPic/20085/26/2008526134946841s.jpg)
网页栅格系统研究(4):技术实现
![](https://img.aspxhome.com/file/UploadPic/200811/6/blueprint_3cols-59s.png)
on error goto (Vbscript)和try catch
css reset中的list-style:none
![](https://img.aspxhome.com/file/UploadPic/20105/26/01-62s.jpg)
用ASP判断客户端浏览器语言自动跳转
关于window.opener的用法
Ext2.0.2经典的一个JS组件(带EXT中文手册)
再谈 Web 默认字体
W3C优质网页小贴士(一)
![](https://img.aspxhome.com/file/UploadPic/up/2008040712541063.gif)
sqlserver中在指定数据库的所有表的所有列中搜索给定的值
asp中使用js的encodeURIComponent
在ASP.NET页面中如何利用JAVASCRIPT脚本向IFRAMES和POPUP传值
10分钟学会Google Map API (二)
X/HTML5 v.s. XHTML2(II)
百度百科中的asp词条:什么是asp
用FrongPage设计网页花样
ASP.NET中MD5和SHA1密码保护算法的使用
DreamweaverMX2004的一句话技巧
HTML和SEO基础知识:H标签全透视
![](https://img.aspxhome.com/file/UploadPic/20109/21/1412060-45s.jpg)