Jump to content
  • 0

Resize iframe height based on the content


Sivad

Question

2 answers to this question

Recommended Posts

  • 0

Hi Sivad,

It is possible by pasting this script on your footer:

<script>

var iframes= document.querySelectorAll("iframe"); //get all iframes
       
iframes.forEach(function f1(elem){ //loop into each iframe

elem.addEventListener("load", function f2(){ //check if each iframe has 'loaded'

elem.style.height =elem.contentWindow.document.body.scrollHeight + 'px'; //assign the height

//if you want to add more to the height, you can do
//elem.contentWindow.document.body.scrollHeight + NUMBER + 'px';


});

});

</script>


If you want to do this on only one iframe when you have multiple:

1.) Assign an ID to the iframe, i.e. id="target"
2.)Use the following script

 

<script>

var iframe1 = document.getElementById("target"); //get iframe with id="target"
       
iframe1.addEventListener("load", function tx(){

iframe1.style.height =iframe1.contentWindow.document.body.scrollHeight + 'px';
    
});

</script>

Please note that this code will not work with a SEARCH FORM as it only resizes WHEN the contents of the iframe have been loaded.

 

Hope this helps.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...