在此过程中,我发现使用iframe是一种“糟糕的做法”。

这是真的吗?使用它们的利与弊是什么?


当前回答

它们并不是不好的做法,它们只是另一种工具,它们增加了灵活性。

For use as a standard page element... they're good, because they're a simple and reliable way to separate content onto several pages. Especially for user-generated content, it may be useful to "sandbox" internal pages into an iframe so poor markup doesn't affect the main page. The downside is that if you introduce multiple layers of scrolling (one for the browser, one for the iframe) your users will get frustrated. Like adzm said, you don't want to use an iframe for primary navigation, but think about them as a text/markup equivalent to the way a video or another media file would be embedded.

对于编写后台事件脚本,通常可以在隐藏的iframe和XmlHttpRequest之间进行选择,以加载当前页面的内容。不同之处在于iframe生成页面加载,因此您可以在大多数浏览器的浏览器缓存中来回移动。注意,谷歌在所有地方都使用XmlHttpRequest,在某些情况下也使用iframe来允许用户在浏览器历史记录中来回移动。

其他回答

不了解它们的缺点就使用它们是“不好的做法”。Adzm的帖子总结得很好。

另一方面,gmail在后台大量使用iFrames来实现一些更酷的功能(比如自动上传文件)。如果你意识到iframe的局限性,我相信你不应该对使用它们感到内疚。

它们并不坏,但实际上很有帮助。我有一个巨大的问题前一段时间,我必须嵌入我的推特饲料,它只是不让md做在同一页上,所以我把它设置在不同的页面,并把它作为一个iframe。

它们还很好,因为所有浏览器(包括手机浏览器)都支持它们。只要你正确地使用它们,它们不能被认为是一种坏习惯。

iframes肯定有用处。你还会怎么把天气网络小部件放在你的页面上?唯一的另一种方法是获取XML并解析它,但是当然,您需要条件来抛出pertenant天气图形……不太值得,但如果你有时间,会更干净。

Based on my experience a positive side for iframe are when calling third party codes, that may involve calling a javascript that calls a has a Document.write(); command. As you may know, these commands cannot be called asynchronously due to how it is parsed (DOM Parser etc). An example of this is http://sourceforge.net/projects/phpadsnew/ I've made use of iframes to help speed up our site as there were multiple calls to phpadsnews and the site was waiting for the response before proceeding to render different parts of the page. with an iframe I was able to allow the site to render other parts of the page and still call the Document.write() command of phpads asynchronously. Preventing and js locking.

当您的主页以HTTP协议加载,而页面的某些部分需要以HTTPS协议工作时,iFrame可以轻松击败jsonp。

特别是,如果你的数据类型不是原生的json,需要在服务器上翻译成json,并在客户端翻译回复杂的html。

所以iFrame并不是邪恶的。