9

I've seen a lot of complaints all over the internet to not use frames, and it's considered bad practice to use frames, plus they are obsolete.

However, iframes are not obsolete, but they are frames. Is it considered bad practice to use them?

Also, can iframes be rendered to data:png/base64 or whatever, in the same way canvases can?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Doge
  • 347
  • 1
  • 4
  • 13
  • Well yea if you have a canvas in the iframe.. :D – T J Jul 06 '14 at 07:10
  • You could take a screenshot of the iFrame and render it as an image =) – Siyah Jul 06 '14 at 07:12
  • The problem with Iframe is the SEO - the search engines might link to the iframe instead doing it to your page. to my opinion, It's not bad practice. As far as I know, You can't convert it to image, but you may get it's content using ajax, and put it into any block object (like div). – Guy Dafny Jul 06 '14 at 07:20
  • With the SEO issue I can use robots.txt – Doge Jul 06 '14 at 07:25
  • Also see [link] (http://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice) – Catwood Sep 30 '14 at 13:33
  • This [answer](http://stackoverflow.com/a/362752/1257607) helped me a lot – DanielV Apr 14 '15 at 12:00
  • Possible duplicate of [Are iframes considered 'bad practice'?](https://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice) – Nisarg Shah Aug 31 '17 at 12:59

3 Answers3

13

However, iframes are not obsolete, but they are frames. Is it considered bad practice to use them?

Sometimes you have to embed a separate HTML document into another, and that is OK. For the record, HTML5 has an entire section dedicated to embedding external content, of which one of the relevant elements is iframe (W3C HTML5).

Of course, whether something is good or bad practice also highly dependent on your use case, but best-practice questions tend to be quite broad by nature.

Also, can iframes be rendered to data:png/base64 or whatever, in the same way canvases can?

As a matter of fact, yes, although IE does not appear to support data:text/html at the moment:

<iframe src="data:text/html,<!DOCTYPE html><html><body><p>abc"></iframe>

However, this is not the "proper" way to do it (even the validators don't agree on whether the syntax is valid — W3C's Nu validator appears to dislike data:text/html because of all the HTML symbols, while Validator.nu only complains about the unencoded whitespace in the DOCTYPE). For embedding raw HTML, you need to use the new srcdoc attribute instead of src (which has even less browser support):

<iframe srcdoc="<!DOCTYPE html><html><body><p>abc"></iframe>

So, in general, specifying raw HTML for iframes is a bad idea for now, and even once browsers start supporting the srcdoc attribute, you should use that over a data URI with the src attribute.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Well, with the data thing I mean specifically rendering the iframe as a base64 encoded image. It doesn't seem possible to me. – Doge Jul 07 '14 at 15:41
  • 1
    @Doge: Given the nature of iframes being dynamic HTML pages and not static graphical images, I'm not entirely sure how that would work in the first place... – BoltClock Jul 07 '14 at 15:56
1

The biggest thing to consider when using iframes, in my opinion, is security issues with 3rd party sites you might include.

For example, in an iframe you include content from a 3rd party. If that 3rd party has been hacked, you are now exposing your users directly to that vulnerability from within your own site. In fact, to your users, the vulnerability is with your site (they probably have no idea about the iframe).

0

The use of iframes is a common test item in Selenium QA interview tests. Using the right command so Selenium can find the content in an iframe can be tricky and cause some confusion while developing test automation.

Epee
  • 1
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 03 '21 at 05:51