Html iframe 是否被视为“不良做法”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/362730/
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
Are iframes considered 'bad practice'?
提问by meleyal
Somewhere along the line I picked up the notion that using iframes is 'bad practice'.
在此期间的某个地方,我发现使用 iframe 是“不好的做法”。
Is this true? What are the pros/cons of using them?
这是真的?使用它们的优点/缺点是什么?
采纳答案by adzm
As with all technologies, it has its ups and downs. If you are using an iframe to get around a properly developed site, then of course it is bad practice. However sometimes an iframe is acceptable.
与所有技术一样,它也有起有落。如果您使用 iframe 来绕过适当开发的站点,那么这当然是不好的做法。但是有时 iframe 是可以接受的。
One of the main problems with an iframe has to do with bookmarks and navigation. If you are using it to simply embed a page inside your content, I think that is fine. That is what an iframe is for.
iframe 的主要问题之一与书签和导航有关。如果您使用它来简单地在您的内容中嵌入一个页面,我认为这很好。这就是 iframe 的用途。
However I've seen iframes abused as well. It should never be used as an integral part of your site, but as a piece of content within a site.
但是,我也看到 iframe 被滥用。它不应被用作您网站的一个组成部分,而应作为网站内的一段内容。
Usually, if you can do it without an iframe, that is a better option. I'm sure others here may have more information or more specific examples, it all comes down to the problem you are trying to solve.
通常,如果您可以在没有 iframe 的情况下完成,那是更好的选择。我相信这里的其他人可能有更多信息或更具体的例子,这一切都归结为您试图解决的问题。
With that said, if you are limited to HTML and have no access to a backend like PHP or ASP.NET etc, sometimes an iframe is your only option.
话虽如此,如果您仅限于 HTML 并且无法访问像 PHP 或 ASP.NET 等后端,有时 iframe 是您唯一的选择。
回答by Tom
They're not bad practice, they're just another tool and they add flexibility.
它们不是不好的做法,它们只是另一种工具,并且增加了灵活性。
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
不影响主页的不良标记中可能会很有用。缺点是,如果您引入多层滚动(一层用于浏览器,一层用于iframe
),您的用户会感到沮丧。就像 adzm 所说的那样,您不想将 aniframe
用于主要导航,而是将它们视为与嵌入视频或其他媒体文件的方式等效的文本/标记。
For scripting background events, the choice is generally between a hidden iframe
and XmlHttpRequest
to load content for the current page. The difference there is that an iframe
generates a page load, so you can move back and forward in browser cache with most browsers. Notice that Google, who uses XmlHttpRequest
all over the place, also uses iframe
s in certain cases to allow a user to move back and forward in browser history.
对于脚本后台事件,通常在隐藏iframe
和XmlHttpRequest
加载当前页面的内容之间进行选择。不同之处在于它会iframe
生成页面加载,因此您可以在大多数浏览器的浏览器缓存中前后移动。请注意,XmlHttpRequest
到处使用的 Googleiframe
在某些情况下也使用s 来允许用户在浏览器历史记录中前后移动。
回答by Chris Van Opstal
It's 'bad practice' to use them without understanding their drawbacks. Adzm's post sums them up very well.
在不了解它们的缺点的情况下使用它们是“不好的做法”。Adzm 的帖子总结得很好。
On the flipside, gmail makes heavy use of iFrames in the background for some of it's cooler features (like the automatic file upload). If you're aware of the limitations of iFrames I don't believe you should feel any compunction about using them.
另一方面,gmail 在后台大量使用 iFrame 来实现一些更酷的功能(例如自动文件上传)。如果您了解 iFrame 的局限性,我认为您不应该对使用它们感到内疚。
回答by user261975
Having worked with them in many circumstances, I've really come to think that iframe's are the web programming equivalent of the goto statement. That is, something to be generally avoided. Within a site they can be somewhat useful. However, cross-site, they are almost always a bad idea for anything but the simplest of content.
在许多情况下与他们合作后,我真的开始认为 iframe 是 web 编程的 goto 语句等价物。也就是说,一般要避免的事情。在站点内,它们可能有些用处。但是,跨站点,除了最简单的内容之外,它们几乎总是一个坏主意。
Consider the possibilities ... if used for parameterized content, they've created an interface. And in a professional site, that interface requires an SLA and version management - which are almost always ignored in rush to get online.
考虑可能性......如果用于参数化内容,他们已经创建了一个界面。而在专业站点中,该界面需要 SLA 和版本管理——这在急于上网时几乎总是被忽略。
If used for active content - frames that host script - then there are the (different) cross domain script restrictions. Some can be hacked, but rarely consistently. And if your framed content has a need to be interactive, it will struggle to do so beyond the frame.
如果用于活动内容 - 承载脚本的框架 - 那么存在(不同的)跨域脚本限制。有些可以被黑客入侵,但很少持续。如果你的框架内容需要交互性,它会很难超越框架。
If used with licensed content, then the participating sites are burdened by the need to move entitlement information out of band between the hosts.
如果与许可内容一起使用,则参与站点需要在主机之间将授权信息移出带外。
So, although, occaisionally useful within a site, they are rather unsuited to mashups. You're far better looking at real portals and portlets. Worse, they are a darling of every web amateur - many a tech manager has siezed on them as a solution to many problems. In fact, they create more.
因此,尽管它们在站点中偶尔有用,但它们不太适合混搭。您最好查看真正的门户和 portlet。更糟糕的是,它们是每个网络业余爱好者的宠儿——许多技术经理都将它们视为许多问题的解决方案。事实上,他们创造了更多。
回答by mel3kings
Based on my experience a positive sidefor 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.
根据我的经验,iframe 的一个积极方面是在调用第三方代码时,这可能涉及调用调用了Document.write();
命令的 javascript 。您可能知道,由于解析方式(DOM 解析器等),无法异步调用这些命令。这方面的一个例子是http://sourceforge.net/projects/phpadsnew/我已经利用 iframes 来帮助加速我们的网站,因为有多次调用 phpadsnews 并且该网站在继续呈现不同的之前正在等待响应页面的一部分。使用 iframe,我能够允许站点呈现页面的其他部分,并且仍然Document.write()
异步调用phpads的命令。防止和js锁定。
回答by djbryson
There are definitely uses for iframes folks. How else would you put the weather networks widget on your page? The only other way is to grab their XML and parse it, but then of course you need conditions to throw up the pertenant weather graphics... not really worth it, but way cleaner if you have the time.
肯定有 iframe 的用途。您还会如何将天气网络小部件放在您的页面上?唯一的另一种方法是获取他们的 XML 并解析它,但是当然你需要条件来抛出相关的天气图形......不是真的值得,但是如果你有时间的话会更干净。
回答by JacquesB
The original frameset model (Frameset and Frame-elements) were very bad from a usability standpoint. IFrame vas a later invention which didn't have as many problems as the original frameset model, but it does have its drawback.
从可用性的角度来看,原始框架集模型(框架集和框架元素)非常糟糕。IFrame 是后来的发明,它没有原始框架集模型那么多的问题,但它确实有它的缺点。
If you allow the user to navigate inside the IFrame, then links and bookmarks will not work as expected (because you bookmark the URL of the outer page, but not the URL of the iframe).
如果您允许用户在 IFrame 内部导航,则链接和书签将不会按预期工作(因为您将外部页面的 URL 设为书签,而不是 iframe 的 URL)。
回答by Jeffz
When your main page loads in HTTP protocol and parts of your page need to work in HTTPS protocol, iFrame can beat jsonp hands down.
当您的主页以 HTTP 协议加载并且部分页面需要在 HTTPS 协议下工作时,iFrame 可以轻松击败 jsonp。
Especially, if your dataType is not natively json and needs to be translated on server into json and translated on client back into e.g. complex html.
特别是,如果您的数据类型不是原生的 json 并且需要在服务器上转换为 json 并在客户端上转换回例如复杂的 html。
So nope - iFrame is not evil.
所以不 - iFrame 不是邪恶的。
回答by Vlad
They are not bad, but actually helpful. I had a huge problem some time ago where I had to embed my twitter feed and it just wouldn't let md do it on the same page, so I set it on a different page, and put it in as an iframe.
它们并不坏,但实际上很有帮助。前段时间我遇到了一个大问题,我不得不嵌入我的 Twitter 提要,但它不允许 md 在同一页面上执行此操作,因此我将其设置在不同的页面上,并将其作为 iframe 放入。
They are also good because all browsers (and phone browsers) support them. They can not be considered a bad practice, as long as you use them correctly.
它们也很好,因为所有浏览器(和手机浏览器)都支持它们。只要您正确使用它们,就不能认为它们是不好的做法。
回答by Brian
It's worth noting that iframes will, regardless of the speed of your users' internet connection or the contents of the iframe, cause a small (0.3s or so) but noticeable slowdown in the speed your page downloads. This is not something you'll see when testing it locally. Actually, this is true for any element added to a page, but iframes seem to be worse.
值得注意的是,无论用户的 Internet 连接速度或 iframe 的内容如何,iframe 都会导致页面下载速度出现小幅(0.3 秒左右)但明显的减慢。这不是您在本地测试时会看到的。实际上,对于添加到页面的任何元素都是如此,但 iframe 似乎更糟。