仅将 HTML 提供给 IE6

发布于 2024-08-05 13:48:52 字数 416 浏览 10 评论 0原文

如果浏览器是IE6/IE7,我需要显示不同的HTML。我知道如果我们只是谈论样式信息,条件注释可以很好地工作,但在这种特殊情况下,它是实际的标记。

我将有一个无序列表的图像,其格式为 png-24。它们将具有圆角(因此需要 png-24 提供的透明度),并且在 CSS(3) 中具有圆角边框样式。由于 IE6 无法识别 png-24,我想要一个备用图像列表,其中 jpg 图像已经包含图像中的边框。这将使 IE6/IE7 满意,同时符合标准的浏览器将获得正确的版本。

(我这样做是因为该网站可能会增长到许多图像,并且需要轻松主题化。如果我在代码中生成边框,只需对边框颜色进行简单更改,它就会改变整个网站。我意识到 IE 仍然会必须是手动的,但如果他们真的希望 IE6 支持,我可以为此单独收费。)

所以我的问题是使用 PHP 执行此操作的最佳方法是什么,有什么缺点吗?

I need to display different HTML if the browser is IE6/IE7. I know conditional comments work fine if we're just talking about styling information but in this particular case it's the actual markup.

I will have an unordered list of images which will be png-24. They will have rounded corners (hence the need for the transparency provided by png-24) and will have rounded border styling in the CSS(3). With IE6 not recognising png-24's I want to have an alternate list of images which are jpg's which already have the borders included in the image. This will keep IE6/IE7 happy whilst the standards compliant browsers will get the proper version.

(I'm doing it this way because the site will potentially grow to many images and it needs to be easily themed. If I produce the borders in code one simple change to border-color and it changes site wide. I realise IE will still have to be manual but I can create a separate charge for this if they really want it supported by IE6.)

So my question is what is the best way to do this using PHP and are there any downsides?

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

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

发布评论

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

评论(9

闻呓 2024-08-12 13:48:52

您可以在 PHP 中检测到它并发送不同的 HTML:

$using_ie6 = ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE) || (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6,0') !== FALSE));
if ($using_ie6) {
    ...
}
else {
    ...
}

此方法的缺点是显然 Opera 有时会在其用户代理标头中发送 MSIE 6,并且这也可能与 IE 移动版匹配。

通常最好尽可能避免将不同的内容发送到不同的浏览器 - 最终您会得到大量重复的代码,并带来所有固有的弊端。

You can detect it in PHP and send different HTML:

$using_ie6 = ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE) || (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6,0') !== FALSE));
if ($using_ie6) {
    ...
}
else {
    ...
}

The downsides to this method are that apparently Opera sometimes sends MSIE 6 in it's user agent headers, and this may also match IE mobile.

Sending different content to different browsers is generally something it's good to avoid, as far as possible - you end up with lots of duplicate code, with all the inherent evils that brings.

回忆那么伤 2024-08-12 13:48:52

使用条件注释 - 它们与标记完美配合,而不仅仅是样式信息:

<!--[if lte IE 6]>
<p>Old school</p>
<![endif]-->

请参阅 MSDN:关于条件评论

Use conditional comments - they work perfectly well with markup, not just with styling information:

<!--[if lte IE 6]>
<p>Old school</p>
<![endif]-->

See MSDN: About Conditional Comments

浮萍、无处依 2024-08-12 13:48:52
$is_ie6 = (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false));
$is_ie6 = (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false));
醉殇 2024-08-12 13:48:52

您可以通过查看 $_SERVER['HTTP_USER_AGENT']

这里是 IE 的 UA 字符串列表。

You can get the Browser UA by looking at $_SERVER['HTTP_USER_AGENT']

Here is a list of UA strings for IE.

忆悲凉 2024-08-12 13:48:52

有什么缺点吗?

哦,是的,非常喜欢。您必须输出一个“Vary”标头来告诉代理/缓存每个 UA 字符串的页面都不同,这会降低缓存的有效性。 (如果不这样做,浏览器将看到带有错误标记的页面。)当然,许多浏览器只是谎报它们的内容。我会尽可能避免 UA 嗅探——而且这几乎总是可能的。

对于用更简单的标记呈现 IE 的特定情况,条件注释要好得多。您可以在 HTML 中的任何位置使用它们,但请注意,MS 建议的下层显示标记语法是无效的。如果仅针对 IE6,请尝试:

<!--[if lt IE 7]>
    (markup for IE6)
<![endif]-->

<!--[if gte IE 7]><!-->
    (markup for everyone else)
<!--<![endif]-->

由于 IE6 无法识别 png-24,我想要一个备用图像列表,其中 jpg 图像已经包含图像中的边框。

您是否尝试过众多 PNG 修复之一来使透明度在 IE6 中正常工作?这比使用单独的图像和标记要容易得多(并且看起来更好)。

are there any downsides?

Oh yes, very much. You have to output a ‘Vary’ header to tell proxies/caches that the page will be different for each UA string, which kills the effectiveness of cacheing. (If you don't, browsers will see pages with the wrong markup.) And of course many browsers just lie about what they are. I would avoid UA sniffing wherever possible — and it almost always is possible.

Conditional comments are much better for the specific case of presenting IE with simpler markup. You can use them anywhere in HTML, but note that the MS-proposed syntax for downlevel-revealed markup is invalid. For targetting something at IE6 only, instead try:

<!--[if lt IE 7]>
    (markup for IE6)
<![endif]-->

<!--[if gte IE 7]><!-->
    (markup for everyone else)
<!--<![endif]-->

With IE6 not recognising png-24's I want to have an alternate list of images which are jpg's which already have the borders included in the image.

Have you tried one of the many PNG fixes to make the transparency work in IE6? This would be much easier (and look better) than having separate images and markup.

风尘浪孓 2024-08-12 13:48:52

如果需要,您可以将其设置为客户端,如下所示:
制作两个 div,一个用于 IE,另一个用于浏览器的其余部分。 IE 的 DIV 将具有 display=none;然后你将添加一个条件CSS,仅适用于IE,并且你将隐藏其他浏览器的div并设置IE display = block

You can make it client-side if you want, like this:
Make two divs, one for IE and the other for the rest of the browser. The DIV for IE will have display=none; Then you will add a conditional CSS, only for IE, and you will hide the div for the other browsers and set the IE display=block

信愁 2024-08-12 13:48:52

只是一个不同的建议,这在 PHP 中也应该是可能的。在 ASP.NET 中,您可以编写特殊的处理程序,该处理程序能够将各种二进制数据发送回客户端。您可以将图像标签链接到特殊的图像处理程序,而不是重写页面。该处理程序将接收请求,检查来自客户端的用户代理字符串以检测浏览器版本,如果是 IE6,它将把 PNG 转换为 JPG 文件并将其流回。在其他情况下,它只会返回 PNG 文件。

您的网址将类似于 http://example.org/yourpage.php?yourimage

基本上,您将在更接近问题根源的地方解决问题。检测IE6已经解释过了。

Just a different suggestion, which should be possible too in PHP. In ASP.NET, you can write special handlers which would be able to send back all kinds of binary data back to the client. Instead of rewriting your pages, you could link the image tags to a special image handler. This handler would receive the request, check the user agent string from the client to detect the browser version and in case of IE6, it would convert the PNG to a JPG file and stream that back. In other cases, it would just return the PNG file.

Your URL would then look a bit like http://example.org/yourpage.php?yourimage

Basically, you would be fixing the problem much closer to the source of the problem. And detecting IE6 has been explained already.

甩你一脸翔 2024-08-12 13:48:52

我知道这不是你问的,但我还是会这么做。

对 IE 6 使用 8 位 PNG 图像。它看起来与 24 位图像不同,但这将使您免于创建额外的标记,只需一个额外的样式表,其中一行替换背景图像。

通过不为有问题的浏览器创建自定义标记,您可以赢得相当多的时间,并且最终结果非常接近您想要的结果 - 特别是如果您当前可以在透明 PNG 中使用不透明 JPEG。

人们确实不应该对 IE6 太好,尽管我(不幸的是)知道并不总是可以做出自己的选择。

I know this isn't what you asked, but I'll hit it anyway.

Use 8-bit PNG-images for IE 6. It won't look the same as 24-bit ones, but that'll save you from creating extra markup, just a extra stylesheet with one line replacing the background-image.

You win quite a bit of time by not creating custom markup for a faulty browser, and the end result is quite close to what you want - especially if you can use opaque JPEGs in transparent PNGs stead currently.

One really shouldn't be too nice to IE6, although I do (unfortunately) know that it's not always possible to make your own choice about it.

盗心人 2024-08-12 13:48:52

是否有可能使用像 javascript 这样的库来实现以下效果:

<!--[if ie]>
 // remove the #for_non_id_browsers and replace, using an ajax call or whatever, with an IE-suitable version
<![endif]-->

<div id="for_non_ie_browsers">
Page content
</div>

Is there any possibility of using something like javascript, a library, to effect the following:

<!--[if ie]>
 // remove the #for_non_id_browsers and replace, using an ajax call or whatever, with an IE-suitable version
<![endif]-->

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