Accessing iframe properties when source is foreign host


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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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">

spederAuthor Commented:
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?

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.