`

document.compatMode介绍

阅读更多

对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途。今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的web页面还是很有帮助,我们都知道,IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有很大差别,而在不声明Doctype的情况下,IE默认又是Quirks Mode。所以为兼容性考虑,我们可能需要获取当前的文档渲染方式。

      document.compatMode正好派上用场,它有两种可能的返回值:BackCompat和CSS1Compat,对其解释如下:
BackCompat Standards-compliant mode is not switched on. (Quirks Mode)
CSS1Compat Standards-compliant mode is switched on. (Standards Mode)

     在实际的项目中,我们还需要在获取浏览是否IE,这样就可以得到IE的渲染模式了。在Ext中的代码:isBorderBox=isIE&&!isStrict。
当文档有了标准声明时, document.compatMode 的值就等于 "CSS1compat", 因此, 我们可以根据 document.compatMode 的值来判断文档是否加了标准声明
var height = document.compatMode=="CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
document.compatMode用来判断当前浏览器采用的渲染方式。

官方解释:
BackCompat:标准兼容模式关闭。
CSS1Compat:标准兼容模式开启。


当document.compatMode等于BackCompat时,浏览器客户区宽度是document.body.clientWidth;
当document.compatMode等于CSS1Compat时,浏览器客户区宽度是document.documentElement.clientWidth。

浏览器客户区高度、滚动条高度、滚动条的Left、滚动条的Top等等都是上面的情况。
一个准确获取网页客户区的宽高、滚动条宽高、滚动条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;
}
 



(以上代码兼容目前流行的全部浏览器,包括:IE、Firefox、Safari、Opera、Chrome)

// JavaScript Document
var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 3 //set x offset of bar in pixels
var startY = 150 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function get_cookie(Name) {
var search = Name + "=";
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}
/*--
function hidebar(){
if (persistclose)
document.cookie="remainclosed=1";
document.getElementById("M").style.display="none";
document.getElementById("show_M").style.display="block";
}
function showbar(){
if(persistclose==0)
document.cookie="remainclosed=0";
document.getElementById("M").style.display="block";
document.getElementById("show_M").style.display="none";
}
--*/
function staticbar(){
barheight=document.getElementById("float_qq").offsetHeight
var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
var d = document;
function ml(id){
   var el=d.getElementById(id);
   if (!persistclose || persistclose && get_cookie("remainclosed")=="")
   el.style.visibility="visible"
   if(d.layers)el.style=el;
   el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
   el.x = startX;
   if (verticalpos=="fromtop")
   el.y = startY;
   else{
   el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
   el.y -= startY;
   }
   return el;
}
window.stayTopLeft=function(){
   if (verticalpos=="fromtop"){
   var pY = ns ? pageYOffset : iecompattest().scrollTop;
   ftlObj.y += (pY + startY - ftlObj.y)/8;
   }
   else{
   var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
   ftlObj.y += (pY - startY - ftlObj.y)/8;
   }
   ftlObj.sP(ftlObj.x, ftlObj.y);
   setTimeout("stayTopLeft()", 10);
}
ftlObj = ml("float_qq");
stayTopLeft();
}

if (window.addEventListener)
window.addEventListener("load", staticbar, false);
else if (window.attachEvent)
window.attachEvent("onload", staticbar);
else if (document.getElementById)
window.onload=staticbar;
 

 

分享到:
评论

相关推荐

    javascript document.compatMode兼容性

    档模式在开发中貌似很少用到,最常见的是就是在获取页面宽高的时候,比如文档宽高,可见区域宽高等。

    document.compatMode的CSS1compat使用介绍

    主要介绍了document.compatMode的CSS1compat使用,需要的朋友可以参考下

    由document.body和document.documentElement想到的

    对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途。其实这个对于我们开发兼容性的web页面还是很有帮助,我们都知道,盒模型的渲染在 Standards Mode和Quirks Mode是有很大...

    取得窗口大小 兼容所有浏览器的js代码

    = “number”){ if(document.compatMode == “number”){ pageWidth = document.documentElement.clientWidth; pageHeight = document.documentElement.clientHeight; }else{ pageWidth = document.body.clie

    jQuery中使用了document和window哪些属性和方法小结

    document.compatMode 获取当前文档的渲染方式。返回值:BackCompat(怪癖模式)和CSS1Compat(标准模式)。 由于IE 8多达五种渲染模式,所以判断是否是怪癖模式需要借助于IE独有的document.documentMo

    基于jquery的获取浏览器窗口大小的代码

    代码如下: function getTotalHeight(){ if($.browser.msie){ return document.compatMode == “CSS1Compat”? document.documentElement.clientHeight : document.body.clientHeight; } else { return self....

    JS 遮照层实现代码

    1.先上效果图:2.使用方法: 初始化:Overlayer.Initialize({ZIndex:100,Backgrund:#666,Opacity:80}); 显示:Overlayer.Show();或Overlayer.Initialize({ZIndex:... if(document.compatMode==’BackCompat’) { obj=d

    ExtJS判断IE浏览器类型的方法

    代码在ext里的src\core\ext.js下... }, isStrict = document.compatMode == “CSS1Compat”, isOpera = check(/opera/), isChrome = check(/chrome/), isWebKit = check(/webkit/), isSafari = !isChrome && check(/saf

    兼容IE/Firefox/Opera/Safari的检测页面装载完毕的脚本Ext.onReady的实现

    var isStrict=document.compatMode==”CSS1Compat”, isOpera=ua.indexOf(“opera”)>-1, isSafari=(/webkit|khtml/).test(ua), isIE=ua.indexOf(“msie”)>-1, isIE7=ua.indexOf(“msie 7”)>-1, isGeck

    页面刷新时记住滚动条的位置jquery代码

    @*点击按钮页面刷新的时候 记住滚动条的位置*@ 代码如下: [removed] [removed] = function () { ... } else if (typeof document.compatMode != ‘undefined’ && document.compatMode != ‘BackCompat’) { scrollPo

    鼠标移到图片上变大显示而不是放大镜效果

    if (document.compatMode==’CSS1Compat’) return document.documentElement.clientHeight; if (document.body) return document.body.clientHeight; return window.undefined; } function getVie

    html5指南-2.如何操作document metadata

    compatMode:获取当前document的兼容模式; cookie:获取或者设置当前document的cookie对象; defaultCharset:获取浏览器默认的编码方式; defaultView:获取当前当前document的window对象; dir:获取或者设置...

    jquery 得到当前页面高度和宽度的两个函数

    代码如下://======需引用jquery框架======// //返回当前页面高度 function pageHeight(){ if($.browser.msie){ return document.compatMode == “CSS1Compat”? document.documentElement.clientHeight : document....

    富文本编辑框

    _QUIRKS = document.compatMode != 'CSS1Compat', _IERANGE = !window.getSelection, _matches = /(?:msie|firefox|webkit|opera)[\/:\s](\d+)/.exec(_ua), _V = _matches ? _matches[1] : '0', _TIME ...

    百度地图毕业设计源码-Front-End-interview-questions:整理收集常见前端面试问题及一些知识点

    百度地图毕业设计源码 1. Front-End-interview-questions 分享请标记出处: ...声明文档的解析类型(document.compatMode),避免浏览器的怪异模式。 如果你是使用最新标准编写的页面但未给出 DOCTYPE 声明,这时就可

    php跳转类 model

    //define('STYLE_PATH',str_replace($_SERVER['DOCUMENT_ROOT'],'',ROOT_DIR)); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ...

    js如何判断不同系统的浏览器类型

    } return { //判断环境,操作系统、浏览器、是否是https连接等 DOC : document, isStrict : DOC.compatMode == “CSS1Compat” , isOpera : check(/opera/) , isChrome : check(/\bchrome\b/) , isWebKit : check(/...

Global site tag (gtag.js) - Google Analytics