简体   繁体   English

动态iframe高度在Chrome中不起作用

[英]Dynamic iframe height not working in chrome

im going crazy here. 我在这里疯了。 i found a script here that was supposedly going to work on chrome as well but i just cant get it to work 我在这里找到了据说也可以在chrome上运行的脚本,但我只是无法使其正常工作

here is the script in my header 这是我头文件中的脚本

<script type="text/javascript">
function resizeFrame() {
     var t=document.getElementById("Footer");
     var f = document.getElementById("mainContent");
     var y = f.contentWindow;
     t.innerHTML = y.document.body.offsetHeight;
     f.height = y.document.body.offsetHeight;
    }
</script>

and the iframe 和iframe

 <iframe onload="resizeFrame()" id="mainContent" src="swwbookpg1.php" scrolling=auto frameborder=0 
height="100%" width="100%">Working!</iframe>
<p id="Footer"> Footer</p>

it works in firefox and IE but not in chrome. 它适用于Firefox和IE,但不适用于chrome。

if anyone can help that would be amazing! 如果有人可以帮助那将是惊人的!

here it is in use: https://www.whalewatchingsydney.com.au/payment_sww/ 此处正在使用: https : //www.whalewatchingsydney.com.au/payment_sww/

thanks =) 谢谢=)

Google seem to be living up to their strapline "do no evil". Google似乎不辜负自己的宗旨“不做恶”。 While Chrome is capable of dynamic iframe height adjustment Google do not make it very easy. 尽管Chrome能够动态调整iframe的高度,但Google却并不十分方便。 After two days of struggling with the problem and trying tons of javascript snippets which worked perfectly in every other Browser but Chrome I finally managed to get something that would work. 经过两天的努力,尝试了无数的javascript代码段,这些代码段在其他所有浏览器中均能正常运行,但Chrome浏览器最终使我获得了一些有用的功能。 I will share this to hopefully save other website developers the 48 hours pain I had to go through. 我将分享此内容,以希望为其他网站开发人员节省我必须经历的48小时痛苦。

Inside external SetiFrameHeight.js file which can then be added to any iframe child document with html. 内部外部SetiFrameHeight.js文件,然后可以使用html将其添加到任何iframe子文档中。

<script type="text/javascript" src="SetiFrameHeigh.js"></script>

setIframeHeight.js setIframeHeight.js

window.onload=setIframeHeight(window.top.document.getElementById('iFrame_ID'));
//note this code only runs serverside when using Google Chrome, very helpful


function setIframeHeight(ifrm){
    var doc = ifrm.contentDocument? ifrm.contentDocument:
    ifrm.contentWindow.document;
    var RestHeight=ifrm.style.height; //Capture original height see why below.
    ifrm.style.visibility = "hidden";
    ifrm.style.height = "10px"; //Necessary to work properly in some browser eg IE
    var NewHeight = getDocHeight( doc ) + 10;
    if (NewHeight>20){
        ifrm.style.height=NewHeight + "px";
    } else { //if dom returns silly height value put back old height see above.
        ifrm.style.height=RestHeight + "px";
    }
    ifrm.style.visibility = "visible";
}

function getDocHeight(doc) {
    doc = doc || document;
    var body = doc.body, html = doc.documentElement;
    var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight,
    html.scrollHeight, html.offsetHeight );
    return height;
}

The real magic of this code snippet is the function getDocHeight which basically tries every conceivable dom combination and selects the one that gives the maximum height. 此代码段的真正魔力是功能getDocHeight,该功能基本上会尝试所有可能的dom组合并选择给出最大高度的组合。 I cannot take credit for this got it from http://www.christersvensson.com/html-tool/iframe.htm . 我对此不敢相信,可以从http://www.christersvensson.com/html-tool/iframe.htm获得

Chrome : 镀铬

  1. I found that when I create an iFrame with document.createElement and assign it a name ("myIframe"), the new iFrame does not load content when I set its location. 我发现,当我使用document.createElement创建一个iFrame并为其分配一个名称(“ myIframe”)时,新的iFrame在设置其位置时不会加载内容。 But if I assign a url to the element's src, it worked fine. 但是,如果我为元素的src分配一个url,它就可以正常工作。 Now then, when instead I manually inserted the iframe tags in the html text (static -- as opposed to using document.createElement) it would load the document when setting its location (and also the tag's src). 现在,当我改为手动将iframe标记插入html文本(静态-与使用document.createElement相对)时,它将在设置其位置(以及标记的src)时加载该文档。 Strange. 奇怪。

  2. Assuming you are intending to display the iframe's content directly, I wonder why? 假设您打算直接显示iframe的内容,我想知道为什么吗? I like to use iframes but really only to dynamically load content to a container in my top window. 我喜欢使用iframe,但实际上仅是将内容动态加载到顶部窗口中的容器中。 The last part of the html loaded into the iframe includes a script that moves the output into the desired location of the top frame. 加载到iframe中的html的最后一部分包括一个脚本,该脚本将输出移至顶部框架的所需位置。

Example of a php script that loads new content via an iFrame. 通过iFrame加载新内容的php脚本示例。

// top Frame calls the php script like this:

 <div id="myContainerDiv">
    <p>old content - please replace me with fresh content</p>
</div>


<iframe name="hiddenFrame" style="display:none;"></iframe>

<script>

    hiddenFrame.location = 'index.php?getNewContent=125';

</script>


// the following script would be at the top of index.php

<?php //

if( isset($_GET['getNewContent']) ):


echo '<div id="myIframeHtml">';

    // the html that I want to load to the top frame..


echo '</div>';

?>
<script>

    newContent = document.getElementById('myIframeHtml').innerHTML; // from this iFrame

    topContainer = top.document.getElementById('myContainerDiv'); // in top frame

    topContainer.innerHTML = newContent; // no fuss-no muss --> no encoding or parsing

</script>

<?php

exit;

endif;

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

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