简体   繁体   English

JavaScript-使用URL片段动态调整iframe跨域的大小

[英]JavaScript - Dynamically resizing iframe cross-domain using URL fragment

I've found a technique that documents cross-domain communication with iframes , and I decided that I'd try to exploit this to accomplish the lofty goal of getting an iframe to resize automatically when the contents change. 我发现了一种记录与iframe进行跨域通信技术 ,因此我决定尝试利用这一技术来实现宏伟的目标,即在内容更改时自动调整iframe的大小。

Basically, here's my plan (I know it's a bit hacky). 基本上,这是我的计划(我知道这有点hacky)。

  • I have control over both the parent page and the child page, but they're going to be loaded on different domains (and, in fact, one will be http and the other https). 我可以控制父页面和子页面,但是它们将被加载到不同的域中(实际上,一个将是http,另一个将是https)。

  • From within the iframe, I have a function that'll run when the document loads, and then periodically thereafter, to get the document's height, and set that as the value of a URL fragment (ie, something like https://blahblah.com/#h-768 ) 在iframe中,我有一个函数,该函数将在文档加载时运行,然后定期运行,以获取文档的高度,并将其设置为URL片段的值(即类似https:// blahblah的值)。 com /#h-768

  • In the parent window, I have a function that'll periodically check the URL of the iframe, grab the hash tag, parse out the height, and change the iframe's height accordingly. 在父窗口中,我有一个函数,该函数将定期检查iframe的网址,获取哈希标签,解析高度,并相应地更改iframe的高度。

The big catch is this: the iframe doesn't have an ID or a name associated with it, and I probably can't change that. 最大的收获是: iframe没有与之关联的ID或名称,我可能无法更改。

In reality, the iframe is initially going to be on the same domain. 实际上,iframe 最初将位于同一域中。 It'll use document.write to create the iframe-polling function in the parent window (because I actually lied earlier when I said I had control over the parent page), and then it'll navigate to the page that I'm actually interested in. 它将使用document.write在父窗口中创建iframe-polling函数(因为我之前说过我对父页面有控制权时实际上在撒谎),然后它将导航到我实际所在的页面有兴趣。

So, the real question is, how can an iframe give itself a name or an ID ? 因此,真正的问题是, iframe如何为自己赋予名称或ID

I've been trying things like this (using jQuery): 我一直在尝试这样的事情(使用jQuery):

$(document).ready(function() {
        // Try changing the name            
        $(self).attr("name", "myframe");
        $(self).attr("id", "myframe");      
});

Trouble is, it doesn't seem to be working. 麻烦的是,它似乎没有用。

Any thoughts? 有什么想法吗?

CLARIFICATION 澄清说明

The parent page will be something like http://mysite.com , and the iframe will initially be http://mysite.com/iframe . 父页面将类似于http://mysite.com ,而iframe 最初将是http://mysite.com/iframe

That initial iframe page is just a dummy page, that: 该初始iframe页面只是一个虚拟页面,即:

  • Gives the <iframe> element on the parent page an ID 为父页面上的<iframe>元素提供ID
  • Creates a new <script> element on the parent page that does the resize polling, and then 在父页面上创建一个新的<script>元素,以进行大小调整轮询,然后
  • Does window.location = "http://my-real-app-location.com" window.location = "http://my-real-app-location.com"

The new page will update the URL fragment with the current height, making the URL of the iframe something like http://my-real-app-location.com/#h-800 (meaning that the document is 800 pixels tall). 新页面将使用当前高度更新URL片段,从而使iframe的URL类似于http://my-real-app-location.com/#h-800 (表示文档高800像素)。

The script that the dummy page created on the parent page needs to grab that URL fragment and change the iframe height accordingly. 虚拟页面在父页面上创建的脚本需要获取该URL片段并相应地更改iframe高度。

Well, the parent page will ultimately control the URL of the iframe. 好吧,父页面最终将控制iframe的URL。 The iframe cannot update anything but its URL. iframe只能更新其网址,而不能更新其他任何内容。

That being said, I'm not sure I understand the problem. 话虽如此,我不确定我是否理解问题。 Why can't you give the iframe an ID when you create it? 创建iframe时为什么不给它一个ID?

An iframe's window/document can't set attributes on it's own container tag, since the tag is owned by the parent document. iframe的窗口/文档无法在其自己的容器标签上设置属性,因为该标签归父文档所有。 The tag in itself has nothing to do with the window and document loaded inside it. 标签本身与里面的窗口和文档无关。

If you need an id attribute on it, you have to set it from the parent document, and there is no way around it. 如果需要id属性,则必须从父文档中进行设置,并且无法解决。

Well, the following is the code that I was originally looking for. 好吧,以下是我最初寻找的代码。 This will give the <iframe> element an ID attribute, and this code runs within the scope of the iframe. 这将为<iframe>元素提供ID属性,并且此代码在iframe的范围内运行。 Of course, it only works if the iframe is on the same domain as the parent page (which, in my case, it initially is). 当然,仅当iframe与父页面位于同一域(在我的情况下,它最初位于)上时,它才起作用。

var foundMyself = false;
$("iframe", parent.document).each(function() {
    if (this.contentWindow == self) {
        $(this).attr("id", "myframe");  
            foundMyself = true; 
    }
});

The following is the code that I'm using to create the appropriate script tag on the parent page (bear in mind that my application sets a fragment #h-xxxxx denoting the height of the page): 以下是我用于在父页面上创建适当的脚本标记的代码(请记住,我的应用程序设置了一个片段#h-xxxxx来表示页面的高度):

if (foundMyself) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.text = "function resizeApp() { var app = document.getElementById(\"myframe\"); var hash = app.contentWindow.location.hash; app.style.height = hash.substring(3); } setInterval('resizeApp();', 500);";

    parent.document.body.appendChild(script);
}

window.location = "https://my-app-real-location.com"

The new problem I'm running into now is that once the iframe has navigated away, I'm not allowed to access the contentWindow property. 我现在遇到的新问题是,一旦iframe移开,就不允许我访问contentWindow属性。

A nice cross domain solution if you looking for is here- A made a whole research post 如果您要寻找的话,一个不错的跨域解决方案就在这里-一篇完整的研究文章

Yet Another cross-domain iframe resize Q&A 另一个跨域iframe调整大小问答

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

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