Solved

Dynamic height of iframe when iframe guest is on a different folder/server from host.

Posted on 2011-09-20
5
306 Views
Last Modified: 2012-05-12
Hello, I am trying to dynamically adjust the height of an iframe on a website and I've found many examples of this online.  But, the guest site is on a completely different server.  My javascript works up until the point where I cross over to the guest's sites dom. My javascript is this:

<script language="JavaScript">
function autoResize(id){
    var newheight;
	
	var frame = document.getElementById(id);
	alert("1");
	var frameWin = frame.contentWindow;
	alert("2");
	var frameDoc = frameWin.document;
	alert("3");
	newheight = frameDoc.body.scrollHeight;
	alert("4");
    document.getElementById(id).height = (newheight) + "px";
    alert("5");
}
</script>

Open in new window


The code gets between alert2 and alert3 and then breaks.

My iframe code is this:

<iframe name="storeFrame" id="storeFrame" src="<?php echo $page; ?>" style="width:1040px; margin-top:-280px; margin-left:-243px; overflow:hidden;" scrolling="no" frameborder="0" onLoad="autoResize('storeFrame');"></iframe> 

Open in new window


I've come to think that it is not letting me access the dom of the guest site because It is on a different server.  Am I correct? or is there a way I can make this work?
0
Comment
Question by:andrewaiello
  • 3
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 36570556
You are right. You cannot access cross domain windows.
But you can do one thing: the iframe ="storeFrame" can tell the parent page its dimensions on its load time to a second invisible iframe by setting its URL with appropriate dimension values as paremeters in the second iframe URL. The second iframe URL needs to have then parent domain realm.
The trick is that you cannot read cross domain URL values but you can set the URL.
And the parent iframe for receiving the dimension values need to have an iframe name.

For example if your second iframe looks like this:
<iframe name="receiverFrame" style="display:none;" onLoad="alert(window.frames.receiverFrame.location.href)" ></iframe>

Then the  page loaded in iframe storeFrame needs to do this to set the iframe receiverFrame URL:
<body onLoad="tellHim=window.open('http://127.0.0.1:8500/ee/receiver.cfm?myheight='+getDocHeight(),'receiverFrame');" >

The script for function etDocHeight() is this:
<script>
function getDocHeight() {
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}
</script>

Open in new window

My web server is ColdFusion but that works also for PHP or ASP or HTML.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36570559
Oh, and of course you do not do simple alert() on reciver side but call some function from the parent page to handle the height parameter.

0
 
LVL 1

Author Comment

by:andrewaiello
ID: 36573770
I don't know if I completely understand what your suggesting.  Are you saying to have the iframe storeFrame with the
<body onLoad="tellHim=window.open('http://127.0.0.1:8500/ee/receiver.cfm?myheight='+getDocHeight(),'receiverFrame');" >

Open in new window

 
included in the page receiver.cfm.  
Then include that page which would reside on my main pages domain in an iframe receiverFrame defined as
<iframe name="receiverFrame" style="display:none;" onLoad="alert(window.frames.receiverFrame.location.href)" ></iframe>

Open in new window

in my main page.

Also for this method to work I would have to have access to the the initial page I am trying to iframe in to my main page to ad the <body onload=""> code. correct?

Thank you
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36573848
Sory, for the confusion.

The last statement is right: if you do not have the access to the loaded page to change it to tell you its dimension then there is no way to read the dimensions. Full stop.

Do you have the posibility to let add that callback into loaded page?
0
 
LVL 1

Author Comment

by:andrewaiello
ID: 36573951
No for some reason we do not have access to the loaded iframe page.  I'm doing this for a client and they have multiple sites but they don't want to manage 2 different stores.  So they wanted to try to iframe the store from their one site into another.  I realized even if I could get the height to be loaded dynamically, when they make selections within the iframe it wouldn't resize anyway. (not without access to the real site at least).  So I just passed url variables and specified the height based on the section with a switch statement.  The only down side is that if they add products to the store and it extends beyond the height, their clients won't see those items.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

760 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

16 Experts available now in Live!

Get 1:1 Help Now