Html 为什么开发人员讨厌 iframe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1081315/
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
Why developers hate iframes?
提问by Amr Elgarhy
Possible Duplicate:
Are iframes considered 'bad practice'?
可能的重复:
iframe 是否被视为“不良做法”?
While working with web developers, I always hear from them that using iframes is something we must avoid as we can, and some say it's something bad, annoying and makes a lot of problems.
在与 Web 开发人员合作时,我总是听到他们说使用 iframe 是我们必须尽可能避免的事情,有些人说它很糟糕,很烦人,并且会产生很多问题。
Also when i told my previous boss "not a developer" one day that i will use iframe, he looked at me as a bad developer :)
此外,有一天当我告诉我的前任老板“不是开发人员”我将使用 iframe 时,他认为我是一个糟糕的开发人员 :)
What i want to know, does iframes have a very bad history with web development?
我想知道的是,iframe 在 Web 开发方面的历史很糟糕吗?
Is it a disaster?
这是一场灾难吗?
In some cases I see that it's a must to use iframes, is saying that means I am a bad developer?
在某些情况下,我认为必须使用 iframe,这是否意味着我是一个糟糕的开发人员?
Or all of that because of it's hard to deal with because of some security issues we must take care about while developing?
或者所有这些都是因为我们在开发时必须注意的一些安全问题而难以处理?
Please list your points if you hate it too or correct me if I am thinking the wrong way.
如果您也讨厌它,请列出您的观点,如果我的想法有误,请纠正我。
回答by Christoph
Iframes can have similar issues as frames and inconsiderate use of XMLHttpRequest
: They break the one-document-per-URL paradigm, which is essential for the proper functioning of the web (think bookmarks, deep-links, search engines, ...).
iframe 可能有与框架类似的问题,并且使用不当XMLHttpRequest
:它们打破了每个 URL 一个文档的范式,这对于网络的正常运行至关重要(想想书签、深层链接、搜索引擎……)。
If you're creating a web application, use whatever technique you want to (including frames, flash, applets, $whatever). If you're creating an actual, informational web page, stick to frameless HTML, CSS and unobstrusive JavaScripts and keep in mind that the page should still be usable with scripting disabled.
如果您正在创建 Web 应用程序,请使用您想要的任何技术(包括框架、flash、applet、$whatever)。如果您正在创建一个实际的信息网页,请坚持使用无框架的 HTML、CSS 和不引人注目的 JavaScript,并记住该页面在禁用脚本的情况下仍然可以使用。
回答by Emil H
As Nosredna said, it's probably because people confuse them with frames, and there are actually a lot of valid arguments against frames. Some of them aren't applicable to iframes, but then again some of them are.
正如 Nosredna 所说,这可能是因为人们将它们与框架混淆了,实际上有很多反对框架的有效论据。其中一些不适用于 iframe,但其中一些适用。
The most striking such issue is probably that of deep linking: It's true that iframes suffer from this to a lesser extent than frames, but if you allow your users to navigate between different pages in the iframe, it will be a problem. There's also a couple of usability problems that you'll have to watch out for. The most common examble is that of double scrollbars, which I personally find incredibly annoying.
最引人注目的此类问题可能是深层链接的问题:的确,iframe 受到的影响比框架要小,但是如果您允许用户在 iframe 中的不同页面之间导航,这将是一个问题。您还必须注意一些可用性问题。最常见的例子是双滚动条,我个人觉得这非常烦人。
I tend to avoid iframes, mostly because I find it to be an unelegant solution. I've found that when I actually sit down and think about it, there's almost always a better solution. Despite that I also believe that there is a place for them. It's the goto of the web world: Just because it has a history of being misused, it has become consensus that it shouldn't ever be used. That really isn't the case here, but I do believe that you should think twice before using iframes.
我倾向于避免使用 iframe,主要是因为我发现它是一个不雅的解决方案。我发现当我真正坐下来思考时,几乎总是有更好的解决方案。尽管如此,我也相信他们有一席之地。它是网络世界的转折点:仅仅因为它有被滥用的历史,它不应该被使用已经成为共识。实际情况并非如此,但我确实相信您在使用 iframe 之前应该三思而后行。
回答by marcc
I wanted to add that most of the time, iframes don't help SEO of a page either. Googlebot doesn't put the content of an iframe on the page.
我想补充一点,大多数时候,iframe 也无助于页面的 SEO。Googlebot 不会将 iframe 的内容放在页面上。
回答by Philippe Leybaert
There is one situation where iframes are (almost) required: when the contents of the iframe is in a different domain, and you have to perform authentication or check cookies that are bound to that domain. It actually prevents security problems instead of creating them.
在一种情况下(几乎)需要 iframe:当 iframe 的内容位于不同的域中时,您必须执行身份验证或检查绑定到该域的 cookie。它实际上防止了安全问题,而不是制造它们。
For example, if you're writing a kind of plugin that can be used on any website, but the plugin has to authenticate on another domain, you could create a seamless iframe that runs and authenticates on the external domain.
例如,如果您正在编写一种可以在任何网站上使用的插件,但该插件必须在另一个域上进行身份验证,您可以创建一个在外部域上运行和身份验证的无缝 iframe。
回答by Nosredna
I think people confuse iframes with HTML frames, and frames are pretty universally despised.
我认为人们将 iframe 与 HTML 框架混为一谈,而框架受到普遍鄙视。
People use iframes all the time without even realizing it. If I recall correctly, TinyMCE uses iFrames.
人们一直在使用 iframe,甚至没有意识到。如果我没记错的话,TinyMCE 使用 iFrames。
回答by ars
One reason is security -- iframe injection attacks were pretty common. See this Ars Technica page for a description:
一个原因是安全性——iframe 注入攻击非常普遍。有关描述,请参阅此 Ars Technica 页面:
http://arstechnica.com/security/news/2008/03/ongoing-iframe-attack-proving-difficult-to-kill.ars
http://arstechnica.com/security/news/2008/03/ongoing-iframe-attack-proving-difficult-to-kill.ars
and another page that summarizes some vulnerabilities (I don't know how many of these are valid for the current crop of browsers, but the article's not that old):
以及另一个总结了一些漏洞的页面(我不知道其中有多少对当前浏览器有效,但文章不是那么旧):
http://www.thespanner.co.uk/2007/10/24/iframes-security-summary/
http://www.thespanner.co.uk/2007/10/24/iframes-security-summary/
On the other hand, they enable cross domain communication, and are used quite commonly by "ajaxy" webapps:
另一方面,它们支持跨域通信,并且被“ajaxy”webapps 广泛使用:
http://softwareas.com/cross-domain-communication-with-iframes
http://softwareas.com/cross-domain-communication-with-iframes
回答by Troy Hunt
One problem is that they have their own page lifecycle so interactivity between host and iframe child is limited (query string, session variables or JS). An alternative would be to consider using a scrolling div.
一个问题是它们有自己的页面生命周期,因此主机和 iframe 子级之间的交互是有限的(查询字符串、会话变量或 JS)。另一种方法是考虑使用滚动 div。
Another problem is printing. The output of an iframe (or a scrolling div for that matter) can be unpredictable and varies wildly between different browsers.
另一个问题是打印。iframe(或与此相关的滚动 div)的输出可能是不可预测的,并且在不同浏览器之间差异很大。
回答by TheRealWorld
There is nothing wrong with Iframes for building web applications. They allow a combination of targeted content with memory encapsulation. How many times did someone build some slick little javascript ajax thing that totally blew chunks when they forgot and uploaded the latest Jlibrary to their parent page or some DIV loaded content? Most of the other issues surround SEO which only matter if you actually wanted to steal someone elses content which is pretty dumb anyway. Iframes give you encapsulated memory and the ability to share well designed pages across multiple sites. Despite what many would have you believe, Iframes and or their equivalents will be around for a very long time.
Iframes 用于构建 Web 应用程序没有任何问题。它们允许将目标内容与内存封装相结合。有多少人构建了一些漂亮的 javascript ajax 小东西,当他们忘记并将最新的 Jlibrary 上传到他们的父页面或一些 DIV 加载的内容时,这些东西完全炸了?大多数其他问题都围绕 SEO,这仅在您确实想窃取其他人的内容时才重要,无论如何,这很愚蠢。iframe 为您提供封装内存和跨多个站点共享精心设计的页面的能力。尽管您相信很多人会相信,Iframe 和/或它们的等价物将存在很长时间。
回答by JonoW
I think IFrames have their place. I wouldn't use them on front-end/public-facing web-sites due to problems with SEO etc. For an internal/back-end web-app I think they are useful when you need to isolate the styling of a particular section from the rest of the page, e.g. a report viewer or an HTML editor, where inherited styles from the parent page could cause a problem if all the content was in one document. My 2c...
我认为 IFrame 有自己的位置。由于 SEO 等问题,我不会在前端/面向公众的网站上使用它们。对于内部/后端 Web 应用程序,我认为它们在您需要隔离特定部分的样式时很有用从页面的其余部分,例如报表查看器或 HTML 编辑器,如果所有内容都在一个文档中,从父页面继承的样式可能会导致问题。我的2c...
回答by Joeri Sebrechts
One reason they're rejected is because they're inherently slow. By the time the iframe begins loading its host page is already in an advanced stage of the loading pipeline. Iframes and snappy browsing are almost impossible to combine.
他们被拒绝的原因之一是他们天生就很慢。当 iframe 开始加载其主机页面时,它已经处于加载管道的高级阶段。iframe 和快速浏览几乎不可能结合起来。