I am using jQuery to control the height of an iframe.
jQuery("iframe",top.document).contents().height();
This works when the height of the iframe increases. When the height decreases it does not work. It returns the old value only. Why is this happening?
I'm not sure if people are satisfied with their methods but I've written up a very very simple approach that seems to work quite well for me, in the newest of Safari, Chrome, IE9, Opera and Firefox.
I had a problem resizing a google calendar fed iFrame so I've just made a default size to it in the iFrame's style: width="600" and height settings of basically the maximum I would want, 980 I believe, then wrapped that in a container Div with height and width at 100% and min-height and min-width at 400px and 300px respectively , and then added some jQuery to run at onload and onresize...
so to clarify, the resize function runs onLoad because I am targetting mobile phones and tablets in my responsive design, which takes the height and width of the calendarCase div and sets the height and width of the iframe (#googleCalendar) accordingly, minus 10 pixes for some padding.
edit I forgot to mention that I've used the caseWidth to set the height of the iFrame in order to keep the proportions constrained and somewhere square.
This has worked fine so far, if anyone has any ideas why it might not be stable please advise,
cheers
It works for me If I set it and retrieve it without using
.contents()
. Please see my example below.EDIT: If I understand correctly, you are trying to get rid of the scroll bars by calling the increment and go back to the original height by calling decrement.
After doing multiple tests across different browsers. Here's the code that works in FF, IE, Chrome, Safari and Opera.
Change your heightIncrement function to use the following:
Change your heightDecrement function to use the following:
If the iframe source is a different domain than its parent, then you will probably need to use easyXDM.
i use this :
This is ancient but hopefully it helps.
Seems like Chrome (or maybe all of webkit, not sure) has a bug where the scrollHeight can increase but not decrease (at least in the case of content in an iframe). So if the content grows, then shrinks, the scrollOffset stays at the higher level. That's not very nice when you are attempting to make the iframe's height just big enough for it's ever-changing content.
A hack workaround is to make it recalculate the height by setting the height to something definitely small enough to cause the iframe's height to be smaller than it's content, then the scrollHeight is reset.
This can cause a slight flicker but normally doesn't, at least, it works on my machine (tm).
I have successfully resize iframe's height and width when it is loaded. basically you can do this as below, and I also post a small article on my blog: http://www.the-di-lab.com/?p=280