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!

asaf
  • 331
  • 2
  • 6
  • 21
  • `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 '12 at 15:52

3 Answers3

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

Community
  • 1
  • 1
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • i did, it still don't fix the problem.. there is more orders that i have to change? – asaf Oct 30 '12 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 '12 at 16:04
  • i have edited the post, with my updated code and the iframe code. – asaf Oct 30 '12 at 16:12
  • It's true but we can't say "with ANY browser" bro. If you close the browser security, you can : http://opensourcehacker.com/2010/11/29/disabling-cross-domain-security-check-for-ajax-development-in-google-chrome/ P.S.: We don't know is that a public page or just for personal use ;) – xecute Oct 30 '12 at 16:13
0

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

Lukas_Skywalker
  • 2,053
  • 1
  • 16
  • 28
  • Check this: http://jsfiddle.net/KWC9x/6/ It's working in the latest IE, Chrome, Opera, Safari and Firefox. – Lukas_Skywalker Oct 30 '12 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 '12 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)​;
xecute
  • 446
  • 8
  • 23