为什么人们仍然使用 iframe? [英] Why do people still use iframes?

查看:60
本文介绍了为什么人们仍然使用 iframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说 iframe 是纯粹的邪恶(好吧,也许不那么纯粹).他们似乎制造了很多麻烦.是的,您的整个网站将加载一次,然后您只需加载单个页面.但是人们为此发明了 AJAX.

For me iframes are pure evil (well, maybe not so pure). They seems to make a lot of troubles. Yes, your whole site will load once and then you can just load single pages. But people invented AJAX for this purpose.

我发现 iframe 的最大问题之一是我无法将链接粘贴到其中一个子页面,因为 URL 从未改变(是的,我知道有一个解决方法)).其次,网络搜索引擎可能会在索引此类页面时遇到问题.

One of the biggest issues I found with iframe was that I couldn't paste a link to one of the subpages, because the URL never changed (yes, I know there is a workaround for this). Second thing, web search engines may have problems indexing such pages.

有时这些网站的可访问性较差,某些浏览器甚至会显示不正确.

Sometimes the accessibility of this sites are worse and some browser can even display them improperly.

有更好的方法来设计没有 iframe 的布局.每天我都能看到有人问这样的问题,比如如何使用 jQuery 访问 iframe?".

There are better ways to design layouts without iframes. Everyday I can see some one asking at SO questions, like "How to access iframe with jQuery?".

那么 iframe 有什么好处呢?仍然使用它们的原因是什么?我只是想知道为什么.

So what are the benefits of iframes? What reason can it be to still use them? I just would like to know why.

推荐答案

我能想到两个原因(目前)人们仍然使用 iframes 而不是 AJAX:

I can think of 2 reasons (at the moment) why people would still use iframes instead of AJAX:

1) iframe 绕过跨域源策略(图像、脚本和样式没有).这对于相对安全地从其他域名中提取站点/内容非常有用.基本上,这提供了一个优势,即能够直观地显示来自其他域的数据,而不会让它们在您的页面上无限制访问(就像 JSONP 那样可以做到).

1) Iframes circumvent the cross domain origin policy (images, scripts, and styles do not). This can be useful for pulling in sites / content from other domain names relatively safely. Basically, this allows the advantage of being able to visually show data from other domains without letting them stomp all over your page with unlimited access (like something like JSONP would be able to do).

2) 您可以从 iframe 中加载多种类型的资源,而不仅仅是某些 mime 类型(您相对限于 application/javascript、application/x-javascript、text/css、text/xml、image/png、图像/jpeg、带有脚本的图像/gif、XHR、图像和来源).例如,如果我想向您展示 PDF,我可以打开一个 iframe 并让 Adob​​e Reader 插件向您展示该文件.此外,在同一个域中,如果我想将脚本、样式和图像全部放在一起(页面内嵌,图像必须是数据 URI),我可以使用 iframe 完成此操作(如果它在相同的域、端口和协议我也可以用 JavaScript 访问).

2) You can load multiple types of resources from within an iframe, not just certain mime-types (you're relatively limited to application/javascript, application/x-javascript, text/css, text/xml, image/png, image/jpeg, image/gif with scripts, XHR, images, and sources). For instance, if I want to show you a PDF, I can open an iframe and let the Adobe Reader plugin show you that file. Additionally, in the same domain, if I want to pipeline a script, style, and image all together (inline on the page, image would have to be data URI), I can accomplish this with an iframe (and if it's in the same domain, port, and protocol I can access it with JavaScript as well).

您知道 Gmail 是一组 iframe 吗?可见的部分只是巧妙的定位.此外,许多 OAuth 实现(Twitter、Facebook、Google、Yahoo!)通常使用 iframe 将其域中的用户与成功的身份验证 URL 相关联(用于在用户登录后).

Did you know that Gmail is a set of iframes? The visible part is just clever positioning. Additionally, many OAuth implementation (Twitter, Facebook, Google, Yahoo!) usually use iframes to associate a user on their domain with a successful authentication URL (for after the user logs in).

这篇关于为什么人们仍然使用 iframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆