97

For me iframes are pure evil (well, maybe not so pure). They seems to make a lot of troubles. Yes, your whole site will load once and then you can just load single pages. But people invented AJAX for this purpose.

One of the biggest issues I found with iframe was that I couldn't paste a link to one of the subpages, because the URL never changed (yes, I know there is a workaround for this). Second thing, web search engines may have problems indexing such pages.

Sometimes the accessibility of this sites are worse and some browser can even display them improperly.

There are better ways to design layouts without iframes. Everyday I can see some one asking at SO questions, like "How to access iframe with jQuery?".

So what are the benefits of iframes? What reason can it be to still use them? I just would like to know why.

2
  • I agree with Moshe above. The only other use of Iframes is for: ancient web clients that do not support modern CSS or in some proprietary environment. Sep 26, 2010 at 9:23
  • these days, unfortunately, every browser have a compatibility problem! they are not standard! div tag has a problem, table tag has a problem and so on... they'll lost our time to design a 100% compatible website. some times the final way to make a website compatible even with IE7. is using an iframe. iframe can patch every problem :'( if you check my website status, you'll see a lot visits from IE7. a lot people are using old browsers even in 2016 !!! Dec 1, 2016 at 7:33

12 Answers 12

109

I can think of 2 reasons (at the moment) why people would still use iframes instead of AJAX:

  1. Iframes circumvent the cross domain origin policy (images, scripts, and styles do not). This can be useful for pulling in sites / content from other domain names relatively safely. Basically, this allows the advantage of being able to visually show data from other domains without letting them stomp all over your page with unlimited access (like something like JSONP would be able to do).

  2. You can load multiple types of resources from within an iframe, not just certain mime-types (you're relatively limited to application/javascript, application/x-javascript, text/css, text/xml, image/png, image/jpeg, image/gif with scripts, XHR, images, and sources). For instance, if I want to show you a PDF, I can open an iframe and let the Adobe Reader plugin show you that file. Additionally, in the same domain, if I want to pipeline a script, style, and image all together (inline on the page, image would have to be data URI), I can accomplish this with an iframe (and if it's in the same domain, port, and protocol I can access it with JavaScript as well).

Did you know that Gmail is a set of iframes? The visible part is just clever positioning. Additionally, many OAuth implementation (Twitter, Facebook, Google, Yahoo!) usually use iframes to associate a user on their domain with a successful authentication URL (for after the user logs in).

3
  • 2
    Re #1, But the HTTP header can block iframe.....
    – Pacerier
    Oct 17, 2017 at 13:28
  • 2
    Re #2, But you can do it without iframe, eg <embed src="http://www.pdf995.com/samples/pdf.pdf" type="application/pdf">
    – Pacerier
    Oct 17, 2017 at 13:31
  • 4
    Re #3, Gmail can be rewritten without iframes. So why do people still use iframes for modern browsers? The question is left unanswered.
    – Pacerier
    Oct 17, 2017 at 13:32
26

IFRAMEs are used to embed and isolate third-party content into a website.

Most of web advertising solutions are based on iframes - because they give security (cross-domain policy) and isolated rectangle on screen which can be fully managed by third party content and scripting (a common use case is advertisments).

Another modern use of IFRAMES is a management of history (common back button workaround) of AJAX applications.

FRAMEs are poor version of IFRAMES. Their use is declining.

1
  • 1
    This is lot simpler, and good example of ads :)
    – oneworld
    Jun 14, 2013 at 15:39
15

If a user has javascript disabled, iframes will work when ajax doesn't. This is not out of the question, considering that people use things like NoScript.

2
  • 4
    This is true, but changing / adding / manipulating the iframe requires JavaScript in most cases (except for 2 I can think of, linking to targets and <form> posting to targets).
    – Dan Beam
    Sep 26, 2010 at 9:36
  • Right; even so, changing what the iframe points to using JS typically requires one line, as opposed to the greater complexity of XMLHttpRequest.
    – Reinderien
    Sep 26, 2010 at 9:50
7

I use them on ajax websites, when I need to upload files without reloading the page.

0
3

I still see iframes being used in large corporations where they provide a single sign on which injects header information about the authenticated user which is then passed, via an iframe, towards the actual application(s). Since the "portal" surrounding the iframe handles all the specific authentication details those applications behind it don't need to have each an implementation for it, making things easier to make for the development team and having a single place to monitor and adjust authentication details of users.

2
  • Don't see why you can't add headers with XMLHttpRequest (w3.org/TR/XMLHttpRequest/#the-setrequestheader-method) unless the domain, protocol, and ports don't match (which means you have a different problem from headers)?
    – Dan Beam
    Sep 26, 2010 at 9:34
  • The point's that it's 2 different applications, usually even 2 completely different technologies. The one handles the authentication and the other grabs the injected headers for the authentication ticket (or building it up when needed). Sep 27, 2010 at 4:42
3

There are plenty of technical reasons to use them (especially the security issue mentioned by Dan Beam).

What you shouldn't do is use iframes “like frames”, doing navigation to new pages by updating the iframe only. As you say, this prevents the navigation from being bookmarkable/linkable, responding to the normal navigation buttons, and providing useful link affordances like open-in-new-tab.

But that's not peculiar to iframes. You can see more and more pages where the navigation is done by fetching new content with XMLHttpRequest and writing it to the main content div's innerHTML. Often this is done with jQuery load() and clever-clever slidey animations. This breaks navigation just as badly as iframe-used-as-frame, or indeed old-school framesets. It's a shame so many web authors are using this tactic believing it to be a super-modern web design methodology, when really it's just a new skin on yesterday's despised framesets.

You can work around it in both cases, but it means you have to store a viewstate in the # fragment identifier part and support proper hash-navigation, which isn't trivial. Even then you've still got problems with non-JS agents like search engines; you end up having to have a parallel ?-based and #-based navigation to support both. It's a pain and most don't bother.

1
  • Look forward to the History API in HTML5, which will allow content fetching without reloading or breaking the back/forward button.
    – Lie Ryan
    Sep 26, 2010 at 11:03
2

Framesets are outdated as of HTML 5, and sometimes you need to have a frame with another site within a site. Also AJAX can only do so much. Try uploading a file to a site on another domain through https without an iframe. AJAX won't help you there.

2

In addition to others reasons, I have one specific usage of iframe in my application. Unfortunately, the target browser in my case is Internet Explorer 6. I need to have a footer and a header that are fixed in my web pages. The main part of this page is scrollable.

However, there is a bug in IE6 where I cannot display a div element on top of select elements using the z-index CSS property. Thus, I need to create an iframe that will be used as a hack to avoid this problem.

Of course, this is a really specific usage of iframe and only concern IE6...

1

Javascript WYSIWYG Editors use iframes, because that is easiest and best way to make it. For example TinyMCE uses it:

http://tinymce.moxiecode.com/

1

I was building a social network and i see iframes being useful for widgets to put on other peoples website to show like a mini profile or integrate with the content on a remote server. Seems like the most simple way to build this. I know some widgets use JavaScript. Also with the iframe method the session is the same as visiting the site like normal, so great for like buttons.

0

Many Formatted Text Editors (e.g. TinyMCE, HTMLArea) are implemented as iframe.

1
  • This is because of FireFox IIRC.
    – alex
    Sep 26, 2010 at 10:33
0

iFrames are okay for some cases, as X-domain-requests, or posting data to a source via parameters. But when I want to access data across domains, I prefer using CSS-files - they can accept params, set cookies, add content to the page (:before & :after) and give a visual feedback.

Not the answer you're looking for? Browse other questions tagged or ask your own question.