As the title, how to resize appened iframe to content? I have already referred other questions:
- jquery get height of iframe content when loaded
- Resize iframe to content with Jquery
- Get height of element inside iframe with jQuery
, but it is still not working. The following are my codes:
$(function() {
$('#appendIframeHere').append("<iframe src=\"http://www.w3schools.com/\"></iframe>");
var iframe = $('#appendIframeHere').find('iframe');
iframe.css("width", "100%");
iframe.css("height", iframe.contents().find('body').height() );
$('#appendIframeHere').text(iframe.contents().find('body').height());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="appendIframeHere"></div>
------------------------------------------------------------update------------------------------------------------------------
I use the same way to get
height()
but it has different value, 0 and 2786. I tried to add a callback function to append()
but I have seen some reference said that append()
complete immediately so I don't have to use callback. After that I also tried promise().done(function(){...})
to append()
but it still get different value from js file and console.