Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto height for Iframe containing embedded / published Google Doc

I have an iframe with a published Google Doc. The contents of that doc are subject to change, so I want to auto adjust the height of the iframe based on its content. I found some solutions for this, but they all require access to the head of the child document. Does anyone have an idea on how to do this?

You can view an excerpt of the code I use below:

#faq{
height: 800px;
overflow: hidden;
position: relative;
width: 660px;
border-top: 1px solid #90C547;
border-bottom: 1px solid #90C547;
}

<div id="faq"><iframe id="faqif" src="https://docs.google.com/document/..../pub?embedded=true" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:900px;width:832px;position:absolute;top:-92px;left:-150px;right:0px;bottom:0px;z-index:0;" height="900px" width="832px"></iframe></div>
like image 861
celalalt Avatar asked Sep 25 '13 16:09

celalalt


People also ask

How do I adjust the iframe height automatically?

How do I adjust the iframe height automatically? Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

How do I resize an embedded Google Doc?

This embedded Google Document with default to the size below. If you wish you change the size of the document, click the grey drop down arrow next to the name of the item and select Edit.

Can you embed iframe in Google Docs?

Copy the "iframe" code. On the page you want the Google Doc embedded, click on "Source Code" button from the WYSIWYG editor to open the HTML editor. Click "OK" and then Submit and Publish your page. Your item should automatically appear on your page as well as any changes you make going forward.


2 Answers

There's no current way to do this.

You can, however, make the height a lot larger and hide the borders, this way the iframe scrollbar won't appear and the document will appear to be a part of your website.

like image 176
Radius Kuntoro Avatar answered Oct 22 '22 00:10

Radius Kuntoro


Simple answer...you cant

(sorry)

The reason is due to the cross domain policy (more, info) you cant access the iframe child document and therefore ascertain its height in order to resize the iframe accordingly, simply put

In computing, the same-origin policy is an important concept in the web application security model. The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number – to access each other's DOM with no specific restrictions, but prevents access to DOM on different sites.

source

[...]

If you don't have control over the framed site, you cannot circumvent the cross-domain policy.

source

And if you cant do this, you cant do what you want because there is no way of ascertaining the child document's height.

It seems the reason you want to do this is design related. As such, you may want to look at different ways to implement the content (iframe) within your site, the obvious one being that the natural restriction on height is browser viewport height, perhaps therefore make the iframe 100% of the viewport (html, body) height? Although this will interfere with your design if there are other components on the page...but there are alternatives...the iframe could:

  1. Be aligned to one side of the page with 100% height set

  2. Be placed within a popup or modal window with 100% height/width

  3. Be controlled (JS) to stretch with the parent window, perhaps with a fixed bottom

Also remember that because this is a global restriction on this kind of content, users are not completely unused to seeing it so though it isnt an ideal design choice, it isnt necessarily one which will confuse/suprise visitors to your site.

like image 37
SW4 Avatar answered Oct 22 '22 00:10

SW4