IFrames (HTML) 过时了吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755795/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:36:26  来源:igfitidea点击:

Are IFrames (HTML) obsolete?

htmliframeobsolete

提问by Peter

Getting contradictory messages about that, hope they're not. I cannot imagine support for it would stop, since a gazillion sites use them.

收到与此相关的矛盾信息,希望不是。我无法想象对它的支持会停止,因为有无数网站在使用它们。

Some additional questions about that:

一些额外的问题:

  1. Why should they phase out this tag?
  2. Any alternative for it?
  1. 他们为什么要逐步淘汰这个标签?
  2. 有什么替代方法吗?

采纳答案by David Kolar

In my opinionthe W3C jumped the gun in dumping iframes from the Strict HTML and XHTML doctypes. In theory you would use the <object>element to add foreign objects to your document, but browser differences and limitations have made this a nonstarter for many developers. With the much-more-pragmatic HTML 5 (which is still a draft), iframes are back and even have two new attributes: seamless, and the intriguing sandbox.

在我看来,W3C 在从 Strict HTML 和 XHTML doctypes 中转储 iframe 方面采取了行动。从理论上讲,您可以使用该<object>元素向文档中添加外来对象,但浏览器的差异和限制使许多开发人员无法做到这一点。有了更实用的 HTML 5(仍是草案),iframe 又回来了,甚至还有两个新属性:seamless和有趣的sandbox.

回答by Koraktor

Support for <iframe>is still there in HTML 5, so I don't think this will change in the near future.

<iframe>HTML 5 的支持仍然存在,所以我认为这在不久的将来不会改变。

To answer your other questions:

回答您的其他问题:

  1. <iframe>s (as frames in general) are most of the time not user-friendly:
    • They don't allow easy access of the content in the frame via an URL (without losing the content outside of the frame at least).
    • Most "technophobe"users are irritated by frames.
    • As far as I know they are slower to render for browsers
  2. Alternatives include dynamic page generation (SSI, PHP, Rails and so on) and using JavaScript / AJAX to change contents of e.g. a <div>
  1. <iframe>s(作为一般框架)大部分时间都不是用户友好的:
    • 它们不允许通过 URL 轻松访问框架中的内容(至少不会丢失框架外的内容)。
    • 大多数“技术恐惧症”用户都被框架激怒了。
    • 据我所知,它们为浏览器呈现的速度较慢
  2. 替代方案包括动态页面生成(SSI、PHP、Rails 等)和使用 JavaScript/AJAX 来更改例如 <div>

To be clear: I'm talking about <iframe>as an interface element. Not a hidden element for loading other stuff like e.g. Google Mail does.

明确地说:我说的<iframe>是一个界面元素。不是用于加载其他内容的隐藏元素,例如 Google Mail。

回答by Thinker

Iframes are obsoletefor page layout. Never use them instead of good CSS layout, even table-based layout is better.

对于页面布局,iframe已过时。永远不要使用它们来代替良好的 CSS 布局,即使是基于表格的布局也更好。

Good reasons for using iframes are:

使用 iframe 的充分理由是:

  • ads: adwords for example uses this technique, it is good for encapsulating - ad css won't destroy your page.
  • hidden iframe: it can be used for hundreds usable things, like tracking, ajax-alternative, etc.
  • 广告:例如,adwords 使用这种技术,它有利于封装 - 广告 css 不会破坏您的页面。
  • 隐藏的 iframe:它可以用于数百种可用的东西,比如跟踪、ajax 替代等。

回答by cgp

IFrames are not obsolete, but the reasons for using them are rare.

IFrame 并没有过时,但使用它们的原因很少。

Reasons for using iframes:

使用 iframe 的原因:

  • It's great for walling off other people's stuff from other domains but it doesn't integrate smoothly. (stylesheets, javascript etc...)
  • Integrating multimedia can sometimes be done easier via an iframe as opposed to using the embed tag.
  • Really, really specialized cases like gmail's case where they are using it for sounds and history management.
  • 它非常适合从其他域中隔离其他人的东西,但它不能顺利集成。(样式表、javascript 等...)
  • 与使用 embed 标签相比,有时可以通过 iframe 更轻松地集成多媒体。
  • 真的,非常专业的案例,例如 gmail 的案例,他们将其用于声音和历史管理。

I would also answer that there is no need for the removal of iframes, it's a needed tag and will be around for a while.

我也会回答说不需要删除 iframe,它是一个必需的标签,并且会存在一段时间。

回答by Gayle

I've seen lots of forums that suggest the Object tag as a replacement for IFrame, which probably works in most cases.

我看到很多论坛都建议使用 Object 标签来替代 IFrame,这可能在大多数情况下都有效。

For example, I had a PDF showing in an IFrame (because there were other things we need to show on the page besides only the PDF) and was able to get it to display fine using Object.

例如,我在 IFrame 中显示了一个 PDF(因为除了 PDF 之外,我们还需要在页面上显示其他内容)并且能够使用 Object.

What was:

什么是:

<iframe id="confirmed_pdf" class="current_pdf" src="/prescriptions/show_pdf?id=123" height="570" width="480"></iframe>

Became:

变成:

<object id="confirmed_pdf" class="current_pdf" data="/prescriptions/show_pdf?id=123" type="application/pdf" height="570" width="480">
  <p>[Show this message if displaying the PDF did not work]</p>
</object>

But Object was not a suitable replacement to fill the requirement to be able to print ONLY the PDF portion of the page.

但是 Object 不是一个合适的替代品来满足只能打印页面的 PDF 部分的要求。

An IFrame is like its own window within the page (a window within a window, basically), and once you get the window object, you can call .print() on it, like:

一个 IFrame 就像它自己在页面中的窗口(基本上是一个窗口中的一个窗口),一旦你得到了 window 对象,你就可以在它上面调用 .print() ,比如:

jQuery("#confirmed_pdf").contentWindow.print();

IFrame has a contentWindow property, that's what makes printing only that part possible. Object does not have a contentWindow property, so there's no way to print only the section of the page.

IFrame 有一个 contentWindow 属性,这使得只打印那部分成为可能。Object 没有 contentWindow 属性,因此无法仅打印页面的一部分。

So, it seems like if you're just using IFrame to display something, there's other tags like Object that can be used instead. But if you need to interact with the contents of the IFrame in certain ways, then IFrame may be necessary.

因此,似乎如果您只是使用 IFrame 来显示某些内容,则可以使用其他标签(例如 Object)来代替。但是如果你需要以某种方式与 IFrame 的内容进行交互,那么 IFrame 可能是必要的。

回答by scunliffe

IFramesare not dead, but Frameset/Framesare dying.

IFrame并没有消亡,但Frameset/Frames正在消亡。

In the last 2 releases of IE(IE7/IE8) zooming in Frames (not IFrames) has created disastrous results.

IE(IE7/IE8)的最后 2 个版本中,放大 Frames(不是 IFrames)造成了灾难性的结果。

By all means use IFrames, but IMHO stay clear of Framesets/Frames.

一定要使用 IFrames,但恕我直言,不要使用 Framesets/Frames。

回答by John Topley

IFrames are used a lot with AJAX. GMail for example, uses nine hidden IFrames I believe.

IFrame 与 AJAX 一起使用很多。例如,我相信 GMail 使用了九个隐藏的 IFrame。

回答by Joshua Carmody

At my previous company, we provided a hosted application that customers would integrate into their own websites. At times, they would use an IFrame to do this, fitting our hosted page into their existing designs. Sometimes this was even done seamlessly (ie. the IFrame had no borders or scrollbars, it just looked like part of the page). I considered this to be a good use of the tag.

在我之前的公司,我们提供了一个托管应用程序,客户可以将其集成到他们自己的网站中。有时,他们会使用 IFrame 来执行此操作,将我们的托管页面融入他们现有的设计中。有时这甚至是无缝完成的(即 IFrame 没有边框或滚动条,它看起来只是页面的一部分)。我认为这是标签的一个很好的用途。

回答by Cruachan

They can be extremely useful in some circumstances, but those are limited. In particular embedding common functionality across multiple sites.

它们在某些情况下可能非常有用,但它们是有限的。特别是在多个站点之间嵌入通用功能。

For example I have a client who runs a number of Scottish goods e-commerce sites. As part of this we have developed a couple of simple applications to locate possible clan names from your surname or your choice of tartans (giggle if you wish but tartans are worth $700 million a year to our economy). The database behind this is surprisingly large (nearly ten thousand rows in the core names and tartans tables) and fairly regularly updated.

例如,我有一个客户经营着许多苏格兰商品电子商务网站。作为其中的一部分,我们开发了几个简单的应用程序来从您的姓氏或您选择的格子呢(如果您愿意,可以咯咯笑,但格子呢每年对我们的经济价值 7 亿美元)中查找可能的氏族名称。这背后的数据库非常大(核心名称和格子呢表中有近万行)并且定期更新。

So we have the applications set up to run on one website and then embedded these into our other websites using an iframe, enabling simple javascript parameter passing so we can integrate the selection of a tartan or clan with functionality on the embedding site. The iframe is set as noborder so it appears completely seamless to the end user.

因此,我们将应用程序设置为在一个网站上运行,然后使用 iframe 将它们嵌入到我们的其他网站中,启用简单的 javascript 参数传递,以便我们可以将格子呢或部落的选择与嵌入网站上的功能集成在一起。iframe 设置为 noborder,因此它对最终用户来说是完全无缝的。

Of course there would be other ways of doing this, but the use of an iframe is simple and robust. And it's certainly not obsolete.

当然,还有其他方法可以做到这一点,但 iframe 的使用既简单又可靠。它当然不会过时。

回答by tsuchan

Horses for courses... <iframe>s are like anything else... for the right purpose they're the right tool; for the wrong purpose they're an ugly hack, or worse.

课程用马…… <iframe> 就像其他任何东西一样……出于正确的目的,它们是正确的工具;为了错误的目的,他们是一个丑陋的黑客,或者更糟。

In Ajax, <div>s are often the more appropriate container. In some places the activity of passing-off external content as part of your own site, as supported by <iframe>s, is inappropriate.

在 Ajax 中,<div> 通常是更合适的容器。在某些地方,将外部内容作为您自己网站的一部分(由 <iframe> 支持)是不合适的。

My team used an <iframe> the other day as an ideal way to give users access to their HTML e-mail history - the e-mails were complete <html> pages which we wanted to insert easily into our web template. <iframe>s were absolutely perfect for presenting that data]'.

前几天,我的团队使用 <iframe> 作为一种理想方式,让用户可以访问他们的 HTML 电子邮件历史记录 - 电子邮件是完整的 <html> 页面,我们希望将其轻松插入到我们的 Web 模板中。<iframe> 非常适合展示这些数据]'。

On the other hand, <iframe>s should almost always be removed or disabled in any user-submitted content which is output back onto the site, because in that context they are a major security issue.

另一方面,在输出回网站的任何用户提交的内容中,几乎总是应该删除或禁用 <iframe>,因为在这种情况下,它们是一个主要的安全问题。