Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accessing iframe properties when source is foreign host

Posted on 2004-10-24
4
Medium Priority
?
196 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
Comment
Question by:speder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

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

Expert Comment

by:Zyloch
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);}
   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
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?

0
 
LVL 36

Accepted Solution

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

618 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