Accessing iframe properties when source is foreign host

Posted on 2004-10-24
Last Modified: 2012-05-05

I've got a website a ( that contains something like this:

<script language="JavaScript" type="text/JavaScript">
function adjustifr()
      document.getElementById('ifr').style.height = ifr.document.body.scrollHeight + 20;
<body onLoad="adjustifr();">
<iframe id="ifr" name="ifr" src="" width="760" marginwidth="0" marginheight="0" frameborder="0"></iframe>

So what I want is to resize the iframe according to its content's length. This throws a "access denied" error presumably because the iframe's source is a foreign host. It works with the source is the localhost.

Is there a way to sort this out? Or perhaps a different strategy?

Question by:speder
    LVL 13

    Expert Comment

    No, you can't access things outside your domain, that's too big a security hole.
    I think using style.overflow="scroll" might achieve what you want. What are you intending to accomplish?
    LVL 36

    Expert Comment

    Hi speder,

    You can try this, although it's not supported by some older browsers, it should work:

    (This uses a <div> instead of an iframe because it can expand. You can set the div borders to look like an iframe with CSS):

    function getPage(url) {
       var xhttp = (document.all) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();"GET", url, false);
       if (document.all) {xHttp.send();} else {xHttp.send(null);}

    Then have this in the <div>

    <div style="width:760px;">
    <script language="javascript">


    Author Comment

    Zyloch> That's a nice idea but unfortunately I need an iframe. Otherwise it won't work with the links on the external site.

    Stormy> What I want is simply the iframe to adjust to the height of the content in order to avoid scroll bars.

    Does it make any difference that I control both domains, i.e. the one with the iframe-tag and the one inside the iframe?

    LVL 36

    Accepted Solution

    Unfortunately, no. Javascript has cross-domain security so you can't do that. Of course, you can always combine my previous idea with <base href=""> and see if that works.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now