Solved

Accessing iframe properties when source is foreign host

Posted on 2004-10-24
189 Views
Last Modified: 2012-05-05
Hi

I've got a website a (www.aaa.com) that contains something like this:

***********************************
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function adjustifr()
{
      document.getElementById('ifr').style.height = ifr.document.body.scrollHeight + 20;
}
//-->
</script>
</head>
<body onLoad="adjustifr();">
<body>
<iframe id="ifr" name="ifr" src="www.bbb.com" width="760" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</html>
</body>
*****************************

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?


0
Question by:speder
    4 Comments
     
    LVL 13

    Expert Comment

    by:StormyWaters
    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?
    0
     
    LVL 36

    Expert Comment

    by:Zyloch
    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);}
       return(xHttp.responseText);
    }

    Then have this in the <div>

    <div style="width:760px;">
    <script language="javascript">
       document.write(getPage("http://www.bbb.com/"));
    </script>
    </div>

    Regards,
    Zyloch
    0
     

    Author Comment

    by:speder
    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?

    0
     
    LVL 36

    Accepted Solution

    by:
    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.
    0

    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 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), 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