views:

236

answers:

2

Hi guys,

I'm building a dummy widget for a iGoogle/Netvibes like portal. This is a "Google Maps" widget, as it only renders a map centered on a specific location.

The widget looks good in all browsers but IE8, in which the height I specify to the <div> that contains the map is not taken into account.

Here's the interesting part of the code:

<body onload="initialize()" >

<div id="map_canvas" style="height:400px; width: 100%;"></div>

</body>

I have no control on the portal, so the only thing I can modify is the widget itself. I also tried to set the height for the <body>, but same thing.

Any idea on why it's not working in IE?

Thanks!

+1  A: 

Put this in the page you're calling with the iframe:

<script type="text/javascript">
    var iframes = window.parent.document.getElementsByTagName('iframe');
    for(var i = 0; i < iframes.length; i ++)
    {
        if(iframes[i].src == window.location)
        {
            iframes[i].style.height = '400px';
        }
    }
</script>

If you are on 2 different domains, this isn't possible, and unfortunately there is no other way when supplying the <iframe> directly to the end-user. The best solution would be to instead give the user a script tag that generates the <iframe> tag using a document.write()

Example script tag to give to your client:

<script type="text/javascript" src="http://www.example.com/widget-getter.js?client=[clientid]&amp;amp;widget=[widgetid]"&gt;&lt;/script&gt;

Contents of the script that the above tag would call:

document.write('<iframe height="400px" src="http://www.example.com/widget.html"&gt;&lt;/iframe&gt;');
orokusaki
Yeah that would surely work, but this part of the code is generated by the portal for each widget, and as I said, I can't modify that :/So there's no way for me to modify the iframe elementBut thanks anyway!
Pedro
@Pedro See my updated answer. This will do it for you.
orokusaki
Hi orokusaki and thanks for your time. Unfortunately, as the iframe and the portal aren't located under the same domain, I can't modify elements such as window.parent.*** - for cross-domain security reasons.
Pedro
@Pedro, see my update
orokusaki
So are you telling me there is no way to have it properly rendered in IE8? Isn't there a known issue about iframe height and IE? Thanks for your time anyway!
Pedro
@Pedro - iframe is intrinsically limited in many ways, but the real problem comes from not having access to the page's source (to add the `height="400px"`, etc. Did you check my latest answer? You can do a `document.write()` to put the iframe in and hand your clients a `<script>` tag instead of handing them the actual `<iframe>`. This is what Google does with AdSense.
orokusaki
That works!So I ended up specifying a fixed height for the iframe (400px), and a height of 100% for the div that contains the map.Not conventional, but effective, thanks orokusaki-sama :)
Pedro
@Pedro no problemo, I'm glad I was able to help.
orokusaki
+1  A: 

Did you try using the height attribute of the iframe tag?

Knu
I wish I could, but the iframe is generated by the portal (an iframe is generated for each widget), and I can't modify that :/
Pedro