使用 IE 条件注释包含 CSS 或 JavaScript 文件会导致更多 http 请求

发布于 2024-08-19 05:45:37 字数 407 浏览 14 评论 0原文

网页设计师为我们创建了带有 IE 特定注释的页面。因此,仅当用户使用特定版本的 IE 时才会包含某些样式表:

<!--[if lt IE 7]>
<link type="text/css" rel="stylesheet" href="/styles/ie6-fixes.css" media="screen"  />
<![endif]-->

同样的技术已用于 JavaScript 文件。 不幸的是,这会导致 IE 用户产生更多 HTTP 请求,因此感知到的页面加载时间会变慢。一般来说,我的做法是将所有 CSS 合并在一个文件中,将所有 JS 合并在一个文件中。

有没有办法将这些特定于 IE 的注释包含在 CSS 和/或 JS 文件本身中?或者有什么方法可以模拟这个功能?

A web designer has created pages for us with IE-specific comments. Consequently, certain stylesheets are only included if the user is using a specific version of IE:

<!--[if lt IE 7]>
<link type="text/css" rel="stylesheet" href="/styles/ie6-fixes.css" media="screen"  />
<![endif]-->

The same technique has been used for JavaScript files.
Unfortunately, this results in more HTTP requests for IE users, so the perceived page load time is slower. Generally, my practice is to combine all CSS in a single file, and all JS in a single file.

Is there a way that these IE-specific comments within CSS and/or JS files themselves? Or is there a way I can simulate this functionality?

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

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

发布评论

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

评论(3

热血少△年 2024-08-26 05:45:37

对于 CSS,您可以使用特定于 IE 的注释将文档内容包围在表单元素中。

<div id="IE6">

这可以让您通过在选择器前面添加“#IE6”来实现 IE6 CSS 修复。

有关更多详细信息,请参阅 http://www.positioniseverything.net/articles/cc-plus。 html

JScript 还具有条件编译功能,可以帮助您将 JS 文件合并为一个。
请参阅 http://msdn.microsoft.com/en-我们/库/ahx1z4fs(VS.80).aspx

For CSS you can use IE-specific comments to surround the document content in an element of the form

<div id="IE6">

This could allow you to implement IE6 CSS fixes by prepending selectors with "#IE6 ".

For more details see http://www.positioniseverything.net/articles/cc-plus.html

JScript also has conditional compilation which may help you consolidate your JS files into one.
See http://msdn.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx

如梦初醒的夏天 2024-08-26 05:45:37

那么你可以做的就是使用动态构建器。例如,在条件中,您可以使用内联脚本将构建的参数添加到 js 数组中。然后你可以使用该数组来构建脚本/css 的 url,例如 /assets/css/build.php?use=base,ie7 以及类似于 js 的东西。

然后,在这个 php(或任何你想要的语言风格)中,你可以使用 minify 或其他一些库将所有脚本/css 编译成单个文件,并去除所有多余内容并交付它们。然后,您还可以缓存构建以便以后更快地交付。

我已经在许多使用 PHP Minify 和/或各种 Symfony 插件执行相同操作的项目中使用了这种策略。

Well what you could do is use a dynamic builder. Fore example in your conditionals you could use inline scripting to add paramters for your build to a js array. then you could use that array to build the url to your script/css like /assets/css/build.php?use=base,ie7 and somethign similar for js.

Then in this php (or whatever flavor of lang you want) you could use minify or some other library to compile all the scripts/css into single files and strip all the cruft and deliver them. You can also then cache the builds for faster delivery later.

Ive used this strategy on a number of projects with PHP Minify and/or Various Symfony plugins that do the same thing.

書生途 2024-08-26 05:45:37

对于 CSS,您可以尝试使用其他浏览器会忽略而 IE6 会尊重的各种选择器技巧,但这会给其他人带来很小的性能成本。

对于 JS,您可以更新函数来检查 User-Agent 字符串,但这与 CSS 提案存在基本相同的问题。

For CSS, you could try to use various selector tricks that other browsers would ignore and IE6 would respect, but this entails a small perf cost for everyone else.

For JS, you could update your functions to check the User-Agent string, but that has basically the same problems as with the CSS proposal.

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