Resize IFRAME Based on Content – Cross Domain
Posted on : 30-04-2009 | By : Michael Fitchett | In : HTML, JavaScript
3
You will find a lot on this topic but no real solid answers. I looked around and ended up piecing this solution together. Yes that’s right it CAN be done! Well sorta…
Put this in between your <HEAD> tags or in your scripts include file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <script type="text/javascript"> //Lookup Cookie by Name and Return Value function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } //Set iFrame Height based on "kofax-iframe-height" value function setIFrameHeightToCookie(cookieValue) { var formHeight = readCookie(cookieValue); var iFrame = document.getElementById('iFrame'); // if (formHeight != null) { iFrame.height = parseInt(formHeight) + 30; } else { iFrame.height = '600px'; } } </script> |
Add the following to the source page inside of the IFRAME
1 2 3 4 5 6 7 8 9 10 11 12 13 | <script type="text/javascript"> if (window.innerHeight && window.scrollMaxY) {// Firefox yWithScroll = window.innerHeight + window.scrollMaxY; xWithScroll = window.innerWidth + window.scrollMaxX; } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac yWithScroll = document.body.scrollHeight; xWithScroll = document.body.scrollWidth; } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari yWithScroll = document.body.offsetHeight; xWithScroll = document.body.offsetWidth; } document.cookie = "page-height=" + yWithScroll + "; expires=01/01/2999 10:00:00; path=/;domain=.yourdomain.com"; </script> |
Create the IFRAME
1 | <iframe id="iFrame" src="http://www.yourdomain.com" width="100%" frameborder="0" scrolling="no" allowtransparency="true" onload="setIFrameHeightToCookie('page-height');"></iframe> |
I searched all over and as a last resort I decided to use a cookie to pass the height over. Since we are working across domains trying to use JavaScript to access another frame is out of the question. You will get a security error that there seems to be no way around.
If you have a better solution please post it in the comments section below and I will update the post.
Hope this helps! It did for me.











this line is throwing a syntax error and I tried it anyway to no avail.
readCookie(name) {
var nameEQ = name + "=";
I am getting empty value from cookie.
alert (ca) => Doesn’t give height and all..
Please share your thought on this
Doesn’t seem like this will work on anything except cross sub-domains. You could accomplish the same thing by just setting document.domain of the iframes on subdomains to be the main domain, then browsers will let you communicate between.
You only have access to the cookies on your domain. And the inner iframe is setting a cookie for “yourdomain” while the outer page is trying to access the cookie by the same name for its domain and not finding anything, which is why its coming back as empty for anyone that tries it on 2 different actual domains.