简体   繁体   English

确定移动浏览器上iframe内iframe父容器的宽度

[英]Determine the width of an iframe's parent container inside the iframe on mobile pbrowsers

Our customers host their own HTML web pages, and we provide some of the content of those pages as embedded iframe elements. 我们的客户托管自己的HTML网页,我们将这些网页的某些内容作为嵌入式iframe元素提供。 (Which means their content and our content have different domain origins.) They typically provide a div element that contains our iframe element. (这意味着它们的内容和我们的内容具有不同的域来源。)它们通常提供一个包含我们的iframe元素的div元素。 (Note that we have no control over their web page content, only over our iframe content.) (请注意,我们无法控制他们的网页内容,只能控制我们的iframe内容。)

The iframe contains JavaScript code that determines the maximum size it can fill, which should be the dimensions of the outer div . iframe包含JavaScript代码,该代码确定它可以填充的最大尺寸,该尺寸应为外部div的尺寸。 This maximum width is used to control (show/hide) scrolling elements within the iframe content. 该最大宽度用于控制(显示/隐藏) iframe内容内的滚动元素。

The problem is that on some mobile browsers , the contents of the iframe is not properly constrained (clipped) to the size/boundaries of its parent <div> . 问题在于,在某些移动浏览器上iframe的内容未正确限制(限制)为其父<div>的大小/边界。 In other words, the dimensions of the iframe are larger (wider) than its parent div . 换句话说, iframe的尺寸比其父div更大(更宽)。 (This is not a problem on desktop browsers.) (这在桌面浏览器上不是问题。)

So the question is : How can the iframe code determine the true proper size of its parent element (especially cross-domain)? 因此,问题是iframe代码如何确定其父元素(尤其是跨域)的正确大小? The best I can get, it seems, is the apparent size of the iframe itself and its children elements. 看来,我能得到的最好的就是iframe本身及其子元素的外观尺寸。

Adding overflow:hidden to the parent div properly hides/clips the iframe content that extends past the div bounds, but the iframe code still cannot determine what that dimension (width) actually is. 在父div添加overflow:hidden可以正确隐藏/ div超出div边界的iframe内容,但是iframe代码仍然无法确定该尺寸(宽度)实际是多少。 (Again, this is apparently not a problem on desktop browsers.) (同样,在桌面浏览器上,这显然不是问题。)

Note: Similar questions (eg, Detect the width of an iframe from within the iframe ) address the problem of accessing the dimensions of the iframe itself from within the iframe ; 注意:类似的问题(例如, 从检测的iframe中的iframe的宽度 )地址访问的尺寸的问题iframe从在其自身iframe ; I need to access the dimensions of the iframe 's parent container. 我需要访问iframe父容器的尺寸。

SOLUTION

I ended up placing the iframe element within its own div element, and accessing the width of that containing element. 我最终将iframe元素放置在其自己的div元素中,并访问了该包含元素的宽度。 (There does not appear to be any way for the iframe to directly access its parent.) iframe似乎无法直接访问其父级。)

Your iframe page has to talk the other pages. 您的iframe页面必须与其他页面对话。 Here is the my example repo on Github, this is what I'am talking... 是我在Github上的示例仓库,这就是我在说的...

EDİT: 编辑:

Every outsource page: 每个外包页面:

function windowWidth(){
    return document.getElementById('content').clientWidth;
}

Iframe page: iframe页面:

window.onload = function(){

    setInterval(function(){

      var iframeWidth = document.getElementById('iframe').contentWindow.windowWidth();
      document.getElementById('iframe').style.width = iframeWidth + "px";

     }, 10);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM