HTML 5 W3C 验证

23
我使用validator.w3.org验证了我的网站。 它报告了以下错误:
Line 5, Column 67: Bad value X-UA-Compatible for attribute http-equiv on element meta.
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >

如果我不包含那个META标签,所有使用IE9浏览器的访问者将以怪异模式(Quirks mode)查看我的网站,我希望避免这种情况。

任何帮助都将不胜感激!


“IE9的访客将会在怪异模式下查看我的网站”…您确定吗? - Wesley Murch
2
是的。没有那个元标签,文档的默认模式将设置为怪异模式。我在不同的计算机上进行了测试。 - šljaker
我想我需要多读一些,因为我以为默认是“IE9”模式。 - Wesley Murch
这是一个重复的问题,请参考:在HTML5中使用IE特定的元标记是否合法? - Eliran Malka
IE9默认使用标准模式,除非在DOCTYPE声明之前存在注释,请参见此处https://dev59.com/0Gw15IYBdhLWcg3wkMnz。我可以确认这一点,因为我没有看到我的浏览器在使用HTML5 doctype时进入怪异模式。 - mare
7个回答

12

我也遇到了同样的问题,但我的解决方案是在我的.htaccess文件中添加以下行:

Header set X-UA-Compatible "IE=edge"

对我来说工作得非常好...


12
你可以将X-UA-Compatible设置放在实际的HTTP头部中。如何做取决于你使用的Web服务器以及是否使用任何服务器端框架。

我正在使用ASP.NET MVC 3。你能给我一些代码示例吗?谢谢! - šljaker
@šljaker - 我不熟悉ASP.NET MVC,但在C#中,它大致类似于Response.AppendHeader("X-UA-Compatible", "IE=edge,chrome=1"); - Alohci
2
你也可以将此添加到web.config文件的system.Webserver > httpProtocol > customHeaders部分。 - ScottE

8

如果你想要IE支持,那么你就必须接受这个事实,即你需要放弃完美的验证分数。

不过没关系,有效性 != 质量


1
你可以设置 <!--[if IE]> xua stuff <![endif]--> 这可能会保持有效性。 - RedRoosterMobile
根据此链接,这个并没有按照预期工作。 - BenMorel

4
对于使用PHP的人来说,通过PHP中的header函数传递此参数的方法如下:
header('X-UA-Compatible: IE=edge,chrome=1');

这是一个带有代码和解释的文章

4

这个解决方案非常简单,主题可以在特征模板中轻松包含它,只需打开/templates/YOUR TEMPLATE/warp/systems/themes/head.php

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<![endif]-->

2

对于使用ASP.NET MVC的开发人员

一种方法是在控制器/操作上使用Action Filter。这会稍微减慢服务器响应速度,但我不知道具体数字。但这是一种干净的方法:

///
/// Represents an attribute that is used to add HTTP Headers to a Controller Action response.
///
public class HttpHeaderAttribute : ActionFilterAttribute
{
    ///
    /// Gets or sets the name of the HTTP Header.
    ///
    /// The name.
    public string Name { get; set; }

    ///
    /// Gets or sets the value of the HTTP Header.
    ///
    /// The value.
    public string Value { get; set; }

    ///
    /// Initializes a new instance of the  class.
    ///
    /// The name.
    /// The value.
    public HttpHeaderAttribute(string name, string value) {
        Name = name;
        Value = value;
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext) {
        if(!filterContext.HttpContext.Response.Headers.AllKeys.Contains(Name, StringComparer.OrdinalIgnoreCase))
            filterContext.HttpContext.Response.AppendHeader(Name, Value);
        base.OnResultExecuted(filterContext);
    }
}

然而,对我而言最好、最干净的方式是使用 web.config。将以下代码放入 <system.webServer> 元素中:

<httpProtocol>
  <customHeaders>
    <!-- 
                            http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
                            Uncomment to serve cross-domain ajax requests

                            <add name="Access-Control-Allow-Origin" value="*" />
                            -->
    <!-- 
                            Force the latest IE version, in various cases when it may fall back to IE7 mode
                            github.com/rails/rails/commit/123eb25#commitcomment-118920
                            Use ChromeFrame if it's installed for a better experience for the poor IE folk 
                            -->
    <add name="X-UA-Compatible" value="IE=Edge,chrome=1" />
    <!-- 
                            Allow cookies to be set from iframes (for IE only)
                            If needed, uncomment and specify a path or regex in the Location directive 

                            <add name="P3P" value="policyref=&quot;/w3c/p3p.xml&quot;, CP=&quot;IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT&quot;" />
                            -->
    <!-- A little extra security (by obscurity) -->
    <remove name="X-Powered-By" />
  </customHeaders>
</httpProtocol>

显然,这仅适用于IIS7+。
希望对你有帮助。

2

你试过不太在意HTML验证器对你的代码的指责吗?这通常对我是有用的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接