Are iframes a terrible idea? [closed]

2019-01-21 19:41发布

I'm building a widget, and I've been using iframes to present content within it. At some point, I might start serving third party HTML and JS, so I thought iframes would be a good idea.

It does make the widget javascript a little more complicated, and I'm concerned that this might not be the best implementation.

Do you have any advice? It would be a huge help to hear what other people think about iframes.

16条回答
趁早两清
2楼-- · 2019-01-21 19:57

Personally, I'd avoid it if you can without too much hassle. Using Javascript (or AJAX if you need to load them dynamically), you can quite easily just use a div and change the contents as necessary - in some cases this will give you much more flexibility and will simplify your JS, especially if there's a lot of interaction between your widget and the rest of the page.

That said, I'd investigate both options, and if the JS path seems too tricky or complicated, just go with iframes.

查看更多
Deceive 欺骗
3楼-- · 2019-01-21 19:58

A good option is to use the overflow CSS property. The default value is visible but you can set it to hidden, scroll or auto. I would use auto in your case. If your content gets too big it will look like you have an iframe but it is still right on the page.

see: overflow property

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-21 19:58

Iframes are not evil they are just another tool like anything else and to determine their merits you have to determine the context in which they will be used. Google Image Search, and several other high profile sites, use iframes for limited purposes.

In general I find they are used for branding or to enable a user to return to a site that redirects the user off site.

Note, if you are using cross domain iframes e.g. an iframe that refers to a domain outside where the page is being served you are limited by design for security reasons and cannot access through javascript the internals of a DOM outside the domain it is associated with.

Also please note many sites prevent their site from being embeded and will stripe the iframe off (redirecting the top url to their domain).

查看更多
仙女界的扛把子
5楼-- · 2019-01-21 20:01

There are several usability and accessability issues with iframes. Some browsers and screenreaders can not display iframes, so you should provide alternative content:

<iframe src="content.html">
    <p>
        This content will only be displayed by browsers that do not support
        iframes. You should provide a link to the content, or in your 
        case an alternative way to use your widget.
    </p>
</iframe>

If you start serving third party content, you should watch out for the iframe grabbing focus after it has finished loading. While a minor annoyance for regular users, it can be very confusing for users browsing with screenreaders.

查看更多
等我变得足够好
6楼-- · 2019-01-21 20:03

No, nothing wrong with iframes. Iframes are probably a better idea if you're going to start serving third party content.

The upcoming HTML5 spec also plans to build more security features into iframes for situations like this, so I would consider it good practice to use them now also.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-21 20:06

Technically there is nothing wronger with iFrame that with alternatives. But semantically, there are evil.

The Web is based on HTTP, a protocol that says a given URL will always leads to a unique ressource.

Using iFrame, you just serve several ressources melted in a web pages behind one URL for all of them. If you have concerns about how the Web should grow, it's troublesome. What's more, for the search engine robots, it's tricky.

查看更多
登录 后发表回答