Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the following jQuery plugin to accomplish this: <br /> <a href="http://benalman.com/projects/jquery-postmessage-plugin/" rel="nofollow">http://benalman.com/projects/jquery-postmessage-plugin/</a></p> <p>Then add the following code to a javascript file that you include inside the iframe document <strong>and</strong> the parent window:</p> <pre><code>var iFrames = []; var iFrameCounter = 0; // Get the parent page URL as it was passed in, for browsers that don't support // window.postMessage var parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')); // The first param is serialized using $.param (if not a string) and passed to the // parent window. If window.postMessage exists, the param is passed using that, // otherwise it is passed in the location hash (that's why parent_url is required). // The second param is the targetOrigin. function SetHeight() { var id = Number(parent_url.replace(/.*#(\d+)(?:&amp;|$)/, '$1')); $.postMessage({ if_height: $(document).outerHeight(true), id: id }, parent_url, parent); }; // Adds the iframe for the given url to the given container function CreateFrame(container, url) { $(function () { // Pass the parent page URL into the Iframe in a meaningful way var src = url + '#' + encodeURIComponent(document.location.href + "#" + iFrameCounter); // Append the iFrame into the DOM. var html = '&lt;iframe id="iFrame' + iFrameCounter + '" src="' + src + '" width="100%" scrolling="no" frameborder="0"&gt;Your browser lacks support for iFrames!&lt;\/iframe&gt;'; iFrames[iFrameCounter] = { id: "iFrame" + iFrameCounter, container: container, if_height: 0 }; // Add iFrame to DOM $(container).append($(html)); iFrameCounter++; }); } $(function () { // Setup a callback to handle the dispatched MessageEvent event. In cases where // window.postMessage is supported, the passed event will have .data, .origin and // .source properties. Otherwise, this will only have the .data property. $.receiveMessage(function (e) { // Get frameId var id = Number(e.data.replace(/.*id=([\d-]*).*/, '$1')); var frame = iFrames[id]; // Get the height from the passsed data. var h = Number(e.data.replace(/.*if_height=([\d-]*).*/, '$1')); if (!isNaN(h) &amp;&amp; h &gt; 0 &amp;&amp; h !== frame.if_height) { // Height has changed, update the iframe. $("#" + frame.id).css("height", (frame.if_height = h)); $("#" + frame.id).css("min-height", (frame.if_height = h)); $("#" + frame.id).css("max-height", (frame.if_height = h)); // Also update the parent element of the iframe. $("#" + frame.id).parent().css("height", (frame.if_height = h)); $("#" + frame.id).parent().css("min-height", (frame.if_height = h)); $("#" + frame.id).parent().css("max-height", (frame.if_height = h)); } }); }); </code></pre> <p>Then use the following code to create the iframe in the parent window:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { CreateFrame("#someiFrameContainer", url); }); &lt;/script&gt; &lt;div id="someiFrameContainer"&gt;&lt;/div&gt; </code></pre> <p>Then everytime the height of the document inside the iFrame changes call this function from the iFrame page:</p> <pre><code>SetHeight(); </code></pre> <p>This will cause the iframe page to send a message containing its new total height to its parent window (supports cross-domain), which will catch the message and update the size of the iframe. This requires that the parent window also includes the script file from above, as this file registers the events and functions to handle these messages automatically when using <code>CreateFrame();</code></p>
    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.
 

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