difference between iframe, embed and object elements

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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?

回答1:

The iframe element represents a nested browsing context. HTML 5 standard - "The 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 's strength is that the embedded code is 'live' and can communicate with the parent document.

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 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).

The 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 element")

Conclusion

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

  • As pointed out in the comments below; scripts in will run but the parent and child contexts can't communicate directly. With 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 or where you would have to set up some other mechanism instead, such as the JavaScript postMessage API.


回答2:

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.



回答3:

Another reason to use object over iframe is that object sub resources (when an 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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!