[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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
?
316 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 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

Technology Partners: 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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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)
Suggested Courses

873 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