浏览器兼容性-iframe高度

2020-08-04  本文已影响0人  栗子daisy

contentDocument:
!DOCTYPE, IE8+支持 contentDocument 属性,其他IE版本请使用 contentWindow 属性。

ownerDocument : Node对象的属性。返回元素根节点文档对象(document 对象)
body
documentElement : document对象的属性,返回的是文档根节点。
scrollHeight


<body>
    <iframe
      src="./1.html"
      width="100%"
      height="100%"
      frameborder="0"
      onload="iFrameHeight(this,40)"
    ></iframe>
  </body>
  <script type="text/javascript" language="javascript">
    function iFrameHeight(objIFrame, intMinHeight) {
      try {
        if (document.compatMode === "CSS1Compat") {
          console.log(document.compatMode);
        } else {
          console.log(document.compatMode);
        }
        if (intMinHeight == null) intMinHeight = 30;
       // if (!!window.ActiveXObject || "ActiveXObject" in window) {
          if (objIFrame.contentDocument) {
          // IE8+支持contentDocument
          var bHeight = objIFrame.contentDocument.body.scrollHeight;
          var dHeight = objIFrame.contentDocument.documentElement.scrollHeight;
        } else {
          // chrome,IE6+支持ownerDocument
          var bHeight = objIFrame.ownerDocument.body.scrollHeight;
          var dHeight = objIFrame.ownerDocument.documentElement.scrollHeight;
        }

        var height = Math.max(bHeight, dHeight);
        objIFrame.height = height + intMinHeight;
        console.log("body", "------", bHeight);
        console.log("documentElement", "------", dHeight);
      } catch (e) {
        window.status = "error" + e.number + e.discription;
      }
    }
上一篇下一篇

猜你喜欢

热点阅读