I am opening my blog page in my website. The problem is I can give a width to an iframe but the height should be dynamic so that there is no scrollbar in the iframe, and it looks like a single page...
I have tried various JavaScript code to calculate the height of the content but all of them give an access denied permission error and is of no use.
<iframe src="http://bagtheplanet.blogspot.com/" name="ifrm" id="ifrm" width="1024px" ></iframe>
Can we use Ajax to calculate height or maybe using PHP?
Fitting IFRAME contents is kind of an easy thing to find on Google. Here's one solution:
This of course doesn't solve the cross-domain problem you are having... Setting
document.domain
might help if these sites are in the same place. I don't think there is a solution if you are iframe-ing random sites.Try this, you can change for even when you want. this example use jQuery.
To directly answer your two subquestions: No, you cannot do this with Ajax, nor can you calculate it with PHP.
What I have done in the past is use a trigger from the iframe'd page in
window.onload
(NOTdomready
, as it can take a while for images to load) to pass the page's body height to the parent.Then the
parent.resizeIframe
looks like this:Et voila, you have a robust resizer that triggers once the page is fully rendered with no nasty
contentdocument
vscontentWindow
fiddling :)Sure, now people will see your iframe at default height first, but this can be easily handled by hiding your iframe at first and just showing a 'loading' image. Then, when the
resizeIframe
function kicks in, put two extra lines in there that will hide the loading image, and show the iframe for that faux Ajax look.Of course, this only works from the same domain, so you may want to have a proxy PHP script to embed this stuff, and once you go there, you might as well just embed your blog's RSS feed directly into your site with PHP.
Here's my solution to the problem using MooTools which works in Firefox 3.6, Safari 4.0.4 and Internet Explorer 7:
Style the "loading" class to show an Ajax loading graphic in the middle of the iframe container. Then for browsers other than Internet Explorer, it will display the full height IFRAME once the loading of its content is complete and remove the loading graphic.
Below is my
onload
event handler.I use an IFRAME within a jQuery UI dialog. Different usages will need some adjustments. This seems to do the trick for me (for now) in Internet Explorer 8 and Firefox 3.5. It might need some extra tweaking, but the general idea should be clear.
I just spent the better part of 3 days wrestling with this. I'm working on an application that loads other applications into itself while maintaining a fixed header and a fixed footer. Here's what I've come up with. (I also used EasyXDM, with success, but pulled it out later to use this solution.)
Make sure to run this code AFTER the
<iframe>
exists in the DOM. Put it into the page that pulls in the iframe (the parent).