1

i looked for a javascript script that will change the height of an iframe, according to the loaded page in it, i found a code but it works for me only in IE and FireFox.. what should i change?

    <SCRIPT type="text/javascript" LANGUAGE="javascript">
    function reSize()
    {
        try{    

        var oBody   =   ifrm.document.body;
        var oFrame  =   document.getElementById("ifrm");

        oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);

        }
        //An error is raised if the IFrame domain != its container's domain
        catch(e)
        {
        window.status = 'Error: ' + e.number + '; ' + e.description;
        }
    }

</SCRIPT>

the iframe code:

<iframe width=940px onload=reSize() src="html/guides.html" id="ifrm" name="iframe_main" frameborder="0"></iframe>

thanks for helping!

1
  • document.all does not exist in firefox or chrome & should cause an error.... You cannot access the document of a frame on another domain in any browser.
    – Alex K.
    Oct 30, 2012 at 15:52

3 Answers 3

2

For starters: document.all has been obsolete for quite some time.

You should use getElementById.

Also if the iframe is not in your domain (e.g. you're in www.mydomain.com and the iframe loads content from www.someotherdomain.com) you WON'T be able to get the iframe's document.body with the normal security settings of most modern browsers... See here for details

4
  • i did, it still don't fix the problem.. there is more orders that i have to change?
    – asaf
    Oct 30, 2012 at 15:55
  • no its from the same domain, and its is working on internet Explorer, the problem is only fireFox and Chrome. someone told that maybe there functions that only IE accept?
    – asaf
    Oct 30, 2012 at 16:04
  • i have edited the post, with my updated code and the iframe code.
    – asaf
    Oct 30, 2012 at 16:12
  • It's true but we can't say "with ANY browser" bro. If you close the browser security, you can : opensourcehacker.com/2010/11/29/… P.S.: We don't know is that a public page or just for personal use ;)
    – xecute
    Oct 30, 2012 at 16:13
0

Try var oBody = ifrm.contentDocument.body || ifrm.contentWindow.document.body;

2
  • Check this: jsfiddle.net/KWC9x/6 It's working in the latest IE, Chrome, Opera, Safari and Firefox. Oct 30, 2012 at 16:16
  • its showing me a link to "show frame content" and then an iframe with 600 width and 400 height. and when i click on show frame content. its giving me the height of the page i have loaded inside. but how i make that the height of the frame will change according to this?
    – asaf
    Oct 30, 2012 at 16:29
0

Give an id to your iframe. I gave "myIframe" and after that use this line in your function instead of yours. it'll work everywhere:

document.getElementById("myIframe").setAttribute("height",600)​;
2
  • i want the iframe to change his height according to the page i load, not to change it to 600
    – asaf
    Oct 30, 2012 at 16:06
  • Oh sorry my mistake. Then @Blackshma's response is true.
    – xecute
    Oct 30, 2012 at 16:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.