简体   繁体   English

JavaScript跨网域iframe调整大小

[英]javascript cross domain iframe resize

I have an iframe pulling in an image and some rows of data (cross domain). 我有一个iframe提取图像和一些数据行(跨域)。 The iframe is created with an imbedded javascript file like this: 使用嵌入的javascript文件创建iframe,如下所示:

    <script type="text/javascript" src="http://www.domain.com/blah/scriptfile.js?blah_id=001" id="blah_script"></script>

The iframe is being created and its loading the images and data perfectly... so far, so good. iframe正在创建中,并且可以完美地加载图像和数据……到目前为止,一切都很好。

Inside the iframe I want to hide the rows of data and shorten the height of the iframe [-] and when the image is clicked [+] expand the height of the iframe and unhide the rows. 在iframe中,我要隐藏数据行并缩短iframe的高度[-],单击图像[+]时,请扩展iframe的高度并取消隐藏行。 I placed the rows in a . 我将这些行放在。

Below is a cut back version of the function being called onclick (and it IS being called). 以下是被称为onclick的函数的简化版本(并且正在被调用)。 The first line in the function shows how I'm generating the szFrameName and the $folder_id is 001 which is reflected in the szSpanName. 函数的第一行显示了如何生成szFrameName,$ folder_id为001,反映在szSpanName中。 When alert() gets called it shows: "blah_iframe_001 blah_cmpnt_001" so the names are correct however the document.getElementById returns null on the blah_iframe but works fine on the blah_cmpnt_ 调用alert()时,它会显示:“ blah_iframe_001 blah_cmpnt_001”,因此名称正确,但是document.getElementById在blah_iframe上返回null,但在blah_cmpnt_上工作正常

function onclickToggle()
{
  var szFrameName = 'blah_iframe_' +"<?php echo $folder_id; ?>";
  var szSpanName = "blah_cmpnt_" +"001";
  var idFrame = document.getElementById(szFrameName);
  var idCmpnt = document.getElementById(szSpanName);

  alert(szFrameName +" " +szSpanName);
}

I've tried window.frameElement but that produces an "access denied" error so how do I resize/shorten the iframe? 我试过window.frameElement,但是会产生“访问被拒绝”错误,那么如何调整iframe的大小或缩短它的大小?

In hopes that this will help someone else, I created 3 files on github that shows exactly how to do this... cross domain iframe resizing at its finest. 为了希望对其他人有所帮助,我在github上创建了3个文件,这些文件确切显示了如何执行此操作。

https://github.com/ppetree/iframe_resize.git https://github.com/ppetree/iframe_resize.git

Having looked a lots of different solutions to this I ended up writing a simple library to take a account of a number of different use cases. 看了很多不同的解决方案后,我最终编写了一个简单的库来考虑许多不同的用例。 As I needed a solution that supported multiple user generated iFrames on a portal page, supported browser resizes and could cope with the host page JavaScript loading after the iFrame. 因为我需要一个在门户网站页面上支持多个用户生成的iFrame的解决方案,所以支持的浏览器会调整大小,并且可以应对在iFrame之后加载主页JavaScript的问题。 I also added support for sizing to width and a callback function and allow the override of the body.margin, as you will likely want to have this set to zero. 我还添加了对宽度调整大小和回调函数的支持,并允许body.margin的覆盖,因为您可能希望将此值设置为零。

https://github.com/davidjbradshaw/iframe-resizer https://github.com/davidjbradshaw/iframe-resizer

The iFrame code is just a little self-contained JavaScript, so that it's a good guest on other people pages. iFrame代码只是一些自包含的JavaScript,因此非常适合其他人页面上使用。

The script is then initialised on the host page and has the following available options. 然后,脚本将在主机页面上初始化,并具有以下可用选项。 More details on what these do on the GitHub page. 有关这些功能的更多详细信息,请访问GitHub页面。

iFrameSizer({
    log: false,
    bodyMargin:null,
    sizeHeight:true,
    sizeWidth:false,
    enablePublicMethods:false,
    interval:33,
    autoResize: true,
    callback:function(messageData){
        $('p#callback').html('<b>Frame ID:</b> ' + messageData.iframe.id + 
                            ' <b>Height:</b> ' + messageData.height + 
                            ' <b>Width:</b> ' + messageData.width +
                            ' <b>Event type:</b> ' + messageData.type);
    }
});

If you set enablePublicMethods, it gives you access in the iframe to manually set the iFrame size and even remove the iframe from the host page. 如果您设置enablePublicMethods,则可让您在iframe中进行访问以手动设置iFrame大小,甚至从主机页面中删除iframe。

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

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