Is there a cross-domain iframe height auto-resizer that works?

后端 未结 7 2360
暖寄归人
暖寄归人 2020-11-27 09:41

I tried a few solutions but wasn\'t successful. I\'m wondering if there is a solution out there preferably with an easy-to-follow tutorial.

相关标签:
7条回答
  • 2020-11-27 10:36

    Here is my simple solution at this page. http://lab.ohshiftlabs.com/iframesize/

    Here is how it works;

    enter image description here

    Basically if you able to edit page at other domain you can place another iframe page that belongs to your server which saving height to cookies. With an interval read cookies when it is updated, update the height of the iframe. That is all.

    Download; http://lab.ohshiftlabs.com/iframesize/iframesizepost.zip

    Edit: 2019 December

    The solution above basically uses another iframe inside of an iframe 3rd iframe is belongs to the top page domain, which you call this page with a query string that saves size value to a cookie, outer page checks this query with some interval. But it is not a good solution so you should follow this one:

    In Top page :

    window.addEventListener("message", (m)=>{iframeResizingFunction(m)});
    

    Here you can check m.origin to see where is it comes from.

    In frame page:

    window.parent.postMessage({ width: 640, height:480 }, "*")
    

    Although, please don't forget this is not so secure way. To make it secure update * value (targetOrigin) with your desired value. Please follow documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

    0 讨论(0)
提交回复
热议问题