Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We had this type of problem, but slightly in reverse to your situation - we were providing the iframed content to sites on other domains, so the <a href="http://en.wikipedia.org/wiki/Same_origin_policy" rel="noreferrer">same origin policy</a> was also an issue. After many hours spent trawling google, we eventually found a (somewhat..) workable solution, which you may be able to adapt to your needs.</p> <p>There is a way around the same origin policy, but it requires changes on both the iframed content and the framing page, so if you haven't the ability to request changes on both sides, this method won't be very useful to you, i'm afraid.</p> <p>There's a browser quirk which allows us to skirt the same origin policy - javascript can communicate either with pages on its own domain, or with pages it has iframed, but never pages in which it is framed, e.g. if you have:</p> <pre class="lang-none prettyprint-override"><code> www.foo.com/home.html, which iframes |-&gt; www.bar.net/framed.html, which iframes |-&gt; www.foo.com/helper.html </code></pre> <p>then <code>home.html</code> can communicate with <code>framed.html</code> (iframed) and <code>helper.html</code> (same domain). </p> <pre class="lang-none prettyprint-override"><code> Communication options for each page: +-------------------------+-----------+-------------+-------------+ | | home.html | framed.html | helper.html | +-------------------------+-----------+-------------+-------------+ | www.foo.com/home.html | N/A | YES | YES | | www.bar.net/framed.html | NO | N/A | YES | | www.foo.com/helper.html | YES | YES | N/A | +-------------------------+-----------+-------------+-------------+ </code></pre> <p><code>framed.html</code> can send messages to <code>helper.html</code> (iframed) but <em>not</em> <code>home.html</code> (child can't communicate cross-domain with parent).</p> <p>The key here is that <code>helper.html</code> can receive messages from <code>framed.html</code>, and <strong>can also communicate</strong> with <code>home.html</code>. </p> <p>So essentially, when <code>framed.html</code> loads, it works out its own height, tells <code>helper.html</code>, which passes the message on to <code>home.html</code>, which can then resize the iframe in which <code>framed.html</code> sits. </p> <p>The simplest way we found to pass messages from <code>framed.html</code> to <code>helper.html</code> was through a URL argument. To do this, <code>framed.html</code> has an iframe with <code>src=''</code> specified. When its <code>onload</code> fires, it evaluates its own height, and sets the src of the iframe at this point to <code>helper.html?height=N</code></p> <p><a href="http://www.quora.com/How-does-Facebook-Connect-do-cross-domain-communication" rel="noreferrer">There's an explanation here</a> of how facebook handle it, which may be slightly clearer than mine above!</p> <p><hr /> <strong>Code</strong></p> <p>In <code>www.foo.com/home.html</code>, the following javascript code is required (this can be loaded from a .js file on any domain, incidentally..):</p> <pre><code>&lt;script&gt; // Resize iframe to full height function resizeIframe(height) { // "+60" is a general rule of thumb to allow for differences in // IE &amp; and FF height reporting, can be adjusted as required.. document.getElementById('frame_name_here').height = parseInt(height)+60; } &lt;/script&gt; &lt;iframe id='frame_name_here' src='http://www.bar.net/framed.html'&gt;&lt;/iframe&gt; </code></pre> <p>In <code>www.bar.net/framed.html</code>:</p> <pre><code>&lt;body onload="iframeResizePipe()"&gt; &lt;iframe id="helpframe" src='' height='0' width='0' frameborder='0'&gt;&lt;/iframe&gt; &lt;script type="text/javascript"&gt; function iframeResizePipe() { // What's the page height? var height = document.body.scrollHeight; // Going to 'pipe' the data to the parent through the helpframe.. var pipe = document.getElementById('helpframe'); // Cachebuster a precaution here to stop browser caching interfering pipe.src = 'http://www.foo.com/helper.html?height='+height+'&amp;cacheb='+Math.random(); } &lt;/script&gt; </code></pre> <p>Contents of <code>www.foo.com/helper.html</code>:</p> <pre><code>&lt;html&gt; &lt;!-- This page is on the same domain as the parent, so can communicate with it to order the iframe window resizing to fit the content --&gt; &lt;body onload="parentIframeResize()"&gt; &lt;script&gt; // Tell the parent iframe what height the iframe needs to be function parentIframeResize() { var height = getParam('height'); // This works as our parent's parent is on our domain.. parent.parent.resizeIframe(height); } // Helper function, parse param from request string function getParam( name ) { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&amp;]"+name+"=([^&amp;#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload