Html 使用 iframe 或 Object 标签将网页嵌入到另一个网页中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/924946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of Iframe or Object tag to embed web pages in another
提问by Tim C
In a web-based system I maintain at work that recently went live, it makes an Object element to embed a second web page within the main web page. (Effectively the main web page contains the menu and header, and the main application pages are in the object)
在我在工作中维护的一个基于 Web 的系统中,它最近上线了,它创建了一个 Object 元素来在主网页中嵌入第二个网页。(实际上主网页包含菜单和标题,主应用程序页面在对象中)
For example
例如
<object id="contentarea" standby="loading data, please wait..."
title="loading data, please wait..." width="100%" height="53%"
type="text/html" data="MainPage.aspx"></object>
Older versions of this application use an IFRAME to do this though. I have found that by using the object tag the embedded web page behaves differently to when it was previously hosted in an IFRAME. In IE, for example, the tool tips don't seen to work (I will post a separate question about this!), and it looks like the embedded page cannot access the parent page in script, although it can if it was an IFRAME.
不过,此应用程序的旧版本使用 IFRAME 来执行此操作。我发现通过使用对象标签,嵌入网页的行为与之前在 IFRAME 中托管时的行为不同。例如,在 IE 中,工具提示不起作用(我将发布一个关于此的单独问题!),并且看起来嵌入的页面无法访问脚本中的父页面,但如果它是 IFRAME 则可以.
I am told the reason for favouring the object tag over the IFRAME is that the IFRAME is being deprecated and so cannot be relied on for future versions of browsers. Is this true though? Is it preferable to use the Object tag over the Iframe to embed web pages? Or is it likely that the IFRAME will be well-supported into the future (long after I am old and grey, and past the useful life of the application I maintain)?
有人告诉我,支持对象标记而不是 IFRAME 的原因是 IFRAME 已被弃用,因此不能依赖于未来版本的浏览器。这是真的吗?在 Iframe 上使用 Object 标签来嵌入网页是否更可取?或者,IFRAME 是否有可能在未来得到很好的支持(在我年老体衰、超过我维护的应用程序的使用寿命之后很久)?
回答by Ionu? G. Stan
The IFRAME elementis part of the upcoming HTML5 standard. Also, HTML5 is developed by the major browser vendors out there (Mozilla, Opera, Safari, IE), that basically makes a guarantee that we will have an IFRAME element in the foreseeable future. Some of them have support for some HTML5 elements already, like AUDIO and VIDEO and some new JavaScript APIs.
在IFRAME元素是部分即将到来的HTML5标准。此外,HTML5 是由主要浏览器供应商(Mozilla、Opera、Safari、IE)开发的,这基本上保证了在可预见的未来我们将拥有 IFRAME 元素。其中一些已经支持某些 HTML5 元素,例如 AUDIO 和 VIDEO 以及一些新的 JavaScript API。
It's also true that the OBJECT element is in the draft, but that's because IFRAME and OBJECT will have different purposes. IFRAMES are mainly designed for sandboxing web applications.
OBJECT 元素在草稿中也是正确的,但那是因为 IFRAME 和 OBJECT 将有不同的用途。IFRAMES 主要是为沙盒 Web 应用程序设计的。
So, my advise is to use IFRAME instead of OBJECT.
所以,我的建议是使用 IFRAME 而不是 OBJECT。
回答by kim3er
IFRAMEs are not part of the XHTML 1.0 Strict DTD. They are totally valid in in HTML 4 and XHTML 1.0 Transitional, I believe. For these reasons alone, IFRAME will continue to be supported for a long time.
IFRAME 不是 XHTML 1.0 Strict DTD 的一部分。我相信它们在 HTML 4 和 XHTML 1.0 Transitional 中完全有效。仅出于这些原因,IFRAME 将在很长一段时间内继续得到支持。
A lot of bookmarklets and analytics code still use IFRAMEs.
许多书签和分析代码仍然使用 IFRAME。
回答by JBE
If you are embedding a HTML page, here is one noticeable difference between iframe
and object
:
如果您要嵌入 HTML 页面,这里是iframe
和之间的一个明显区别object
:
- with
iframe
updatingsrc
will change the browser history (adding a new entry) - with
object
updatingdata
will not change the browser history
- 与
iframe
更新src
将改变浏览器历史记录(添加新条目) - 与
object
更新data
不会改变浏览器历史记录
Also it seems like drag&drop does not work if the page is embedded in the object
tag, but works in the iframe
tag. I noticed it personally using react-draggable
, and I can see someone had the same issue (https://stackoverflow.com/questions/31807848/replacing-iframe-with-object-tag-drag-and-drop-not-working)
如果页面嵌入在object
标签中,但在iframe
标签中工作,拖放似乎也不起作用。我注意到它亲自使用react-draggable
,我可以看到有人有同样的问题(https://stackoverflow.com/questions/31807848/replacing-iframe-with-object-tag-drag-and-drop-not-working)
回答by DanSingerman
Although the W3C specs may indicate that the IFRAME tag is being deprecated, (in XHTML at least anyway), browser developers do not necessarily follow exactly what those specs say (IE6 anyone?)
尽管 W3C 规范可能表明 IFRAME 标签已被弃用(至少在 XHTML 中),但浏览器开发人员不一定完全遵循这些规范所说的(IE6 任何人?)
As use of IFRAMEs is so prevalent at the moment, and the W3C can't seem to decide if they are part of the future or not (HTML 4.01 vs XHTML), I am pretty sure they are the safer implementation to use for almost every browser.
由于目前 IFRAME 的使用非常普遍,而且 W3C 似乎无法决定它们是否是未来的一部分(HTML 4.01 与 XHTML),我很确定它们是几乎所有使用的更安全的实现浏览器。