iframes and HTML5 templates have significant hurdles with older Internet Explorer versions. For Internet Explorer 8 and older, the frameBorder and scrolling attributes must be set on the iframe as the equivalent CSS properties cannot accomplish the same thing. Unfortunately, these properties are not valid in HTML5.
To make matters worse, changes to the frameBorder attribute have no effect once the tag has been parsed (or added to the DOM for dynamic elements). So the typical workaround of adding the non-compliant attributes through script won’t work either. Today I finally came up with a workaround for this issue and was able to have my standards compliant iframe tag and make it work in older Internet Explorer versions:
<iframe id="MyIframe" name="MyIframe" src="http://my.iframe.src/"></iframe> <script> if(document.attachEvent) { var OldIframe = document.getElementById("MyIframe"); var NewIframe = OldIframe.cloneNode(true); //no scrollbars - overflow:hidden works for other browsers NewIframe.setAttribute("scrolling", "no"); // IE8 and lower add a special border on frames. // It's actually in addition to the css border property. if(!document.addEventListener) { NewIframe.setAttribute("frameBorder", 0); } OldIframe.parentNode.replaceChild(NewIframe, OldIframe); } </script> |
All I’m doing is making a copy of the iframe, setting the new attributes, and replacing the original iframe in the DOM with my new one. The document.addEventListener check will succeed on almost any modern browser and is an effective way to target IE8 and older (although there are numerous other and more exact ways to do this).
And now I have my standards compliant iframe tag and have it work in Internet Explorer 7 and 8.
Update 5/24/2011: After more testing it appears IE9 also requires similar treatment to remove scrollbars. The code sample above has been updated.
- Personal and Course Websites to be Discontinued October 1 - June 21, 2024
- How catalog changes will affect your academic website - May 13, 2024
- Partial Site Outage 4-30-24 - April 30, 2024
- Modern Campus Calendar Details - April 2, 2024
- Calendar updates: Training available - March 20, 2024
- Updates: New Modern Campus Calendar - March 7, 2024
- Preview MSU’s new calendar system - January 22, 2024
- Recap: Hello Omni CMS, goodbye Web Press! - March 14, 2023
- Improve your Omni CMS skills at this free training - February 21, 2023
- Celebrate Global Accessibility Awareness Day (GAAD) on May 19 - May 16, 2022