1

I am wondering if theres some way to make it so that if there is a bunch of content in an iFrame it will just resize the document essentially so it fits. Making all the content inside the iFrame smaller. To clarify I want to resize the content, not the iFrame's size itself.

Any ideas how I would do this?

I have tried out some stuff but so far no luck.

Script:

<script type="text/javascript">
function autoResize(id){
    var newheight;
    var newwidth;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
        newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
    }

    document.getElementById(id).height= (newheight) + "px";
    document.getElementById(id).width= (newwidth) + "px";
}
</script>

iFrame:

<iframe id="iframehtml" style="width:100%;height:100%;" onLoad="autoResize('iframe1');"></iframe>

By the way the reason there is no src set for the iFrame is because I have it in JavaScript so when you click a a button it will edit the html of the iFrame via jQuery.

2 Answers 2

1

You can use load event to resize iframe

var iframe = document.getElementByID('your-iframe');
iframe.addEventListener('load', function() {
  iframe.style.height = iframe.contentWindow.document.body.offsetHeight + 'px';
})
Sign up to request clarification or add additional context in comments.

1 Comment

Check my comment on aruuu's answer :)
0

You should be able to achieve this using jQuery .contents() -

assuming id is the id of the element you wish to change within the iframe:

var frame = $('#iframehtml')
frame.contents().find(id).width = (newheight) + "px";

See http://forum.jquery.com/topic/changing-elements-in-an-iframe

6 Comments

The iFrame still has scroolbars when I click that.
What is the content within the iframe? Is id the id of the iframe itself, or the id of the element you want to change within the iframe?
Well, there isn't actually a source of the iFrame, thats the issue. As I stated the content in the iFrame is actually just set using .contents().html('test') for example. Its not actually a source. Thats why I don't believe I can use a load function.
why are you using .contents.html('test') instead of $('#iframehtml').src = 'test'?
That doesn't make much sense at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.