Accessing iframe properties when source is foreign host

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

I've got a website a (www.aaa.com) 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="www.bbb.com" 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
  • 2
LVL 13

Expert Comment

ID: 12393886
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

ID: 12393900
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();
   xHttp.open("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

ID: 12394510
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

Zyloch earned 300 total points
ID: 12394530
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="http://www.bbb.com/"> and see if that works.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

600 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