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

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?
LVL 1
andrewaielloAsked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
andrewaielloAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
andrewaielloAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.