Somewhere along the line I picked up the notion that using iframes is 'bad practice'.
Is this true? What are the pros/cons of using them?
Somewhere along the line I picked up the notion that using iframes is 'bad practice'.
Is this true? What are the pros/cons of using them?
When your main page loads in HTTP protocol and parts of your page need to work in HTTPS protocol, iFrame can beat jsonp hands down.
Especially, if your dataType is not natively json and needs to be translated on server into json and translated on client back into e.g. complex html.
So nope - iFrame is not evil.
They're not bad practice, they're just another tool and they add flexibility.
For use as a standard page element... they're good, because they're a simple and reliable way to separate content onto several pages. Especially for user-generated content, it may be useful to "sandbox" internal pages into an
iframe
so poor markup doesn't affect the main page. The downside is that if you introduce multiple layers of scrolling (one for the browser, one for theiframe
) your users will get frustrated. Like adzm said, you don't want to use aniframe
for primary navigation, but think about them as a text/markup equivalent to the way a video or another media file would be embedded.For scripting background events, the choice is generally between a hidden
iframe
andXmlHttpRequest
to load content for the current page. The difference there is that aniframe
generates a page load, so you can move back and forward in browser cache with most browsers. Notice that Google, who usesXmlHttpRequest
all over the place, also usesiframe
s in certain cases to allow a user to move back and forward in browser history.Based on my experience a positive side for iframe are when calling third party codes, that may involve calling a javascript that calls a has a
Document.write();
command. As you may know, these commands cannot be called asynchronously due to how it is parsed (DOM Parser etc). An example of this is http://sourceforge.net/projects/phpadsnew/ I've made use of iframes to help speed up our site as there were multiple calls to phpadsnews and the site was waiting for the response before proceeding to render different parts of the page. with an iframe I was able to allow the site to render other parts of the page and still call theDocument.write()
command of phpads asynchronously. Preventing and js locking.It's 'bad practice' to use them without understanding their drawbacks. Adzm's post sums them up very well.
On the flipside, gmail makes heavy use of iFrames in the background for some of it's cooler features (like the automatic file upload). If you're aware of the limitations of iFrames I don't believe you should feel any compunction about using them.
There are definitely uses for iframes folks. How else would you put the weather networks widget on your page? The only other way is to grab their XML and parse it, but then of course you need conditions to throw up the pertenant weather graphics... not really worth it, but way cleaner if you have the time.