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

x
?
Solved

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

Posted on 2011-09-20
5
Medium Priority
?
315 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
[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
  • 3
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

650 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