274

HTML5 defines several embedded content elements, which, from a bird's-eye view, seem to be very similar to the point of being largely identical.

What is the actual difference between iframe, embed and object?

If I want to embed an HTML file from a third-party site, which of these elements could I use, and how would they differ?

5
  • they don't really look identical at all. third party site would be an iframe.
    – Kai Qing
    May 21, 2013 at 1:06
  • 1
    embed vs object subset: stackoverflow.com/questions/1244788/embed-vs-object, iframe vs object subset: stackoverflow.com/questions/924946/…, embed vs object vs video: stackoverflow.com/questions/11199048/… Aug 27, 2014 at 11:23
  • 3
    @KaiQing Whether they look identical or not is your opinion, obviously not shared by the OP. Oh, and by the way, it is not an answer to his question, either. Aug 25, 2016 at 17:00
  • 2
    @Malik - that's why it's a comment. And no, looking identical is not my opinion since the definitions of each clearly outline the differences and when you would use each one. How they appear on the front end could theoretically look identical but I think the OP's context was in usage and not appearance. The comment is to illustrate that you would use an iframe for third party, as they asked. I can only assume I was too busy to write out a full answer back then.
    – Kai Qing
    Aug 25, 2016 at 18:15
  • 4
    Now MDN has a detailed explanation developer.mozilla.org/en-US/docs/Learn/HTML/…
    – toshi
    Jun 8, 2019 at 5:30

5 Answers 5

180

<iframe>

The iframe element represents a nested browsing context. HTML 5 standard - "The <iframe> element"

Primarily used to include resources from other domains or subdomains but can be used to include content from the same domain as well. The <iframe>'s strength is that the embedded code is 'live' and can communicate with the parent document.

<embed>

Standardised in HTML 5, before that it was a non standard tag, which admittedly was implemented by all major browsers. Behaviour prior to HTML 5 can vary ...

The embed element provides an integration point for an external (typically non-HTML) application or interactive content. (HTML 5 standard - "The <embed> element")

Used to embed content for browser plugins. Exceptions to this is SVG and HTML that are handled differently according to the standard.

The details of what can and can not be done with the embedded content is up to the browser plugin in question. But for SVG you can access the embedded SVG document from the parent with something like:

svg = document.getElementById("parent_id").getSVGDocument();

From inside an embedded SVG or HTML document you can reach the parent with:

parent = window.parent.document;

For embedded HTML there is no way to get at the embedded document from the parent (that I have found).

<object>

The <object> element can represent an external resource, which, depending on the type of the resource, will either be treated as an image, as a nested browsing context, or as an external resource to be processed by a plugin. (HTML 5 standard - "The <object> element")

Conclusion

Unless you are embedding SVG or something static you are probably best of using <iframe>. To include SVG use <embed> (if I remember correctly <object> won't let you script†). Honestly I don't know why you would use <object> unless for older browsers or flash (that I don't work with).

† As pointed out in the comments below; scripts in <object> will run but the parent and child contexts can't communicate directly. With <embed> you can get the context of the child from the parent and vice versa. This means they you can use scripts in the parent to manipulate the child etc. That part is not possible with <object> or <iframe> where you would have to set up some other mechanism instead, such as the JavaScript postMessage API.

5
  • 5
    embed is ideal to get the visitor to follow a redirect chain originating at a website that blocks framing. (We use it to kick off federated login.)
    – Riking
    Jun 30, 2015 at 19:46
  • 7
    Not true about "object won't let script".schepers.cc/svg/blendups/embedding.html
    – Dzenly
    Oct 2, 2015 at 16:09
  • 1
    @Dzenly The scripts declared in, for example, and SVG included via <object> will run, but it's not possible (or I didn't manage) to get at the context of the object from the parent page. So "internal" scripts will run, "external" scripts, from the point of view of the object, can't communicate with the objects context. Nov 1, 2015 at 16:22
  • <embed> is really outdated. I wouldn't use it for anything anymore. Today every major browser is able to use object for every plugin possible. If you want to use flash and define its type instead of a CLSID it will work in every browser the same way. It can even run java applets. However, I would still use iframes to embed external pages.
    – Bachsau
    Mar 12, 2017 at 2:49
  • 5
    @Bachsau since this is a discussion about different options and their tradeoffs it feels wrong to just blankly say <iframe /> is the way to go. The entire point of the post is that they are all different. <embed> is still in the spec: w3.org/TR/html5/embedded-content-0.html#the-embed-element, so mentioning it is justified. I also find it more than a little funny that you argue that <embed> is outdated and mention Java applets in the next sentence :) Mar 14, 2017 at 9:30
36

One reason to use object over iframe is that object re-sizes the embedded content to fit the object dimensions. most notable on safari in iPhone 4s where screen width is 320px and the html from the embedded URL may set dimensions greater.

1
  • 14
    Can you kindly give more details and/or references? Otherwise, this only qualifies as a comment, not as an answer.
    – cnst
    Jan 19, 2016 at 17:09
7

Another reason to use object over iframe is that object sub resources (when an <object> performs HTTP requests) are considered as passive/display in terms of Mixed content, which means it's more secure when you must have Mixed content.

Mixed content means that when you have https but your resource is from http.

Reference: https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content

1
  • That doesn't seem to be the case based on current reading of the linked article, which lists object under both active and passive headings. Passive: "subresources (when an <object> performs HTTP requests)" / Active: "<object> (data attribute)" (the latter is how you load another HTTP request as per the original question.
    – Tim Abell
    Nov 9, 2017 at 14:20
4

iframe have "sandbox" attribute that may block pop up etc

0

iframe has many advantages - it's public than embed and object. Most browsers support iframe rather than embed and object so use iframe to avoid problems and don't forget most of the sites don't allow you to embed their websites in your website like facebook but others allow it like wikipedia, youtube.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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