IE 条件注释会减慢页面加载速度吗?

发布于 2024-08-13 01:11:16 字数 404 浏览 9 评论 0原文

最近我已经多次就“CSS hacks”与“条件注释”争论了,我想我应该把这个问题扔给 Stack Overflow 社区。

<!--[if IE]>
 <link type="text/css" rel="stylesheet" href="ie-specific.css" /> 
<![endif]-->

反对使用 IE 条件注释的主要论点似乎是您在每个页面加载上添加额外的 HTTP 请求,从而减慢页面的显示速度。我无法找到任何现实世界的指标来证明或反驳这一说法。

撇开维护多个样式表与一个样式表的争论不谈(尽管这本身就是一个有效的讨论),这里是否有人做过任何测试来确定使用条件注释给您带来多少减速,或者能够指出我想知道其他人收集的任何统计数据吗?

I've been having the "CSS hacks" vs "Conditional Comments" argument enough times lately that I thought I'd throw this question out to the Stack Overflow community.

<!--[if IE]>
 <link type="text/css" rel="stylesheet" href="ie-specific.css" /> 
<![endif]-->

The main argument against using IE Conditional Comments seems to be that you're adding additional HTTP requests on each pageload, thus slowing down the display of your page. What I haven't been able to find are any real-world metrics either proving or disproving this statement.

Leaving aside the argument of maintenance of multiple stylesheets vs. one stylesheet (though that's a valid discussion in and of itself), has anyone here done any testing to determine just how much of a slowdown using Conditional Comments gives you, or be able to point me in the direction of any stats anyone else has collected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-08-20 01:11:16

这实际上并不是关于条件注释本身,它就像条件编译。

IE 浏览器会将您的页面视为包含内容

<link type="text/css" rel="stylesheet" href="ie-specific.css" /> 

(进行一点点额外处理来评估评论条件)。

非 IE 浏览器只能看到那里的评论。

因此,唯一潜在的性能影响是 IE 浏览器需要评估条件注释条件(可以忽略不计),然后包含另一个 CSS 文件。当然,其他浏览器只会看到通用文件并完全忽略注释。

接下来的问题是,为 IE 提供(例如)5Kb 标准兼容的 CSS 文件,然后提供(例如)2Kb“hacks”文件,并让其他人只获得 5Kb 文件,这有什么优点和缺点?与为每个人提供一个 7Kb 文件,其中包含可疑的 CSS 相比。根据各种因素,包括网络速度和延迟、文件大小、整个页面使用的资源数量等,这可能会或可能不会对 IE 情况下的性能产生明显影响(毫无疑问,它会更快)对于非 IE 用户代理)。

与所有性能问题一样,您必须在您的环境中分析您的代码,以了解影响是什么 - 但作为一般准则,我预计对 IE 的影响可以忽略不计,性能很小对于所有其他浏览器都会增加,以及能够在实际文件中编写“正确的”CSS 然后单独修复 IE 的模糊感觉。

This isn't really about conditional comments per se, it's like conditional compilation.

An IE browser will see your page as including

<link type="text/css" rel="stylesheet" href="ie-specific.css" /> 

(with a tiny bit of extra processing to evaluate the comment condition).

A non-IE browser will just see a comment there.

So the only potential performance impact is that IE browsers will need to evaluate the conditional comment condition (which is going to be negligible), and then is going to include another CSS file. While of course other browsers just see the generic file and completely ignore the comment.

The question then is what the pros and cons of serving IE a (say) 5Kb standards-compliant CSS file followed by a (say) 2Kb "hacks" file, and having everyone else get just the 5Kb file; vs serving everyone a 7Kb file with dodgy CSS in it. Depending on various factors including network speed and latency, the size of the file, the number of resources used on the page overall, etc., this may or may not make a noticeable impact on performance in the IE case (it will unquestionably be faster for non-IE user agents).

As with all performance questions, you'll have to profile your code in your environment to see what the impact is - but as a general guideline I would expect the impact to be negligible for IE, a small performance increase for all other browsers, as well as the fuzzy feeling being able to write "proper" CSS in your actual file and then fix IE separately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文