0

I'm looking to resize an iFrame on a page once its contents are loaded. Unfortunately the content being loaded in the iFrame are from a different domain. I've tried to use the following code to call a function in the parent window but it's throwing security errors:

<body onload="parent.document.someFunction(document.body.scrollHeight);">

Is there a reliable way to pull this off using content from other domains (we have access to these domains and can FTP) or do I need to tell the client they have to mirror the content on their own domain?

Brandon Durham
  • 7,096
  • 13
  • 64
  • 101

1 Answers1

0

Please see this post and my answers: Calling a parent window function from an iframe

It is possible, but not easy. You have to hack about a bit to get it to work.

I believe that, because you have access to the framed domain and frame page domains, you will be able to use the document.domain-in-the-head trick.

<script>
    document.domain = "mydomain.com";
</script>

Update:

Can I just check the following?:

  1. Have you put the document.domain <script> in the head of both pages?

  2. Do both have the exact same string, i.e. both have "subdomain.mydomain.com" or both have "mydomain.com"? Typically you use this to ensure that the framed page has the same document domain as the parent.

  3. Do you still get a security error after calling a parent function from the framed page?

  4. If not, this is the function I use to resize my frame. It is located in the head of the framed page.

    window.ResizeFrame = function (newHeight) {
        if (window.parent && window.parent.document) {
            var $frame = $(window.parent.document).find("#frame-id");
    
            if ($frame.length) {
                if (typeof (newHeight) === "number") {
                    $frame.css("height", newHeight);
                }
            }
        }
    };
    

Let me know how it goes!

Community
  • 1
  • 1
Ash Clarke
  • 4,807
  • 1
  • 37
  • 48