How to make a meta tag the first one in the <he

2019-01-13 16:36发布

I'm using JSF2, GlassFish 3.1, PrimeFaces 2.x.

I'm having strange rendering problems on IE9. I'm supposed to be able to force IE9 to render as IE9 by inserting the following:

<html>
<head>
  <!-- Enable IE9 Standards mode -->
  <meta http-equiv="X-UA-Compatible" content="IE=9" />
...

But the thing is, it's not working because (I'm told) the meta tag MUST be the first tag in the section.

When I do this in my XHTML file ...

<html ...>

<f:view contentType="text/html" locale="#{loginHandler.currentLocale}">

<h:head>
    <!-- Enable IE9 Standards mode -->
    <meta http-equiv="X-UA-Compatible" content="IE=9" />

The resulting HTML looks like this, where JSF/PrimeFaces has inserted a bunch of "link" and "script" tags before my new meta tag.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="/orcf-webui/javax.faces.resource/jquery/ui/jquery-ui.css.jsf?ln=primefaces&amp;v=2.2" />
<link type="text/css" rel="stylesheet" href="/orcf-webui/javax.faces.resource/wijmo/wijmo.css.jsf?ln=primefaces&amp;v=2.2" />
<script type="text/javascript" src="/orcf-webui/javax.faces.resource/jquery/jquery.js.jsf?ln=primefaces&amp;v=2.2"></script>
<script type="text/javascript" src="/orcf-webui/javax.faces.resource/jquery/ui/jquery-ui.js.jsf?ln=primefaces&amp;v=2.2"></script>
<!-- Enable IE9 Standards mode -->
<meta http-equiv="X-UA-Compatible" content="IE=9" />

Is there any way to get my meta tag in the right place so it will work? (Or an alternative way to make this IE9 problem go away?

5条回答
倾城 Initia
2楼-- · 2019-01-13 16:46

Just to comment on your answer and previous comments:

HTTP Header and HTML Head are not completly different things (effectively) if you view page on IE8 as shown by diagram here. If you set HTTP header, but not HTML Head, the directive from HTTP header is still taken into account.

I don't know how IE9 behaves, but I guess that in a similar way.

查看更多
\"骚年 ilove
3楼-- · 2019-01-13 16:52

You can create Filter which adds header:

X-UA-Compatible: IE=9

to response object.

Source

查看更多
神经病院院长
4楼-- · 2019-01-13 17:02

You might want to switch from Mojarra to MyFaces. Looking at the source code of MyFaces' HEAD renderer - first gets rendered the content of the element and then other resources. Mojarra is doing this probably other way around. If you don't want to switch JSF implementations you can just implement your own HEAD element renderer.

However I would suggest just to find out why IE9 is not working without the X-UA-Compatible meta tag. It is supposed to make newer versions to behave like older versions.

查看更多
地球回转人心会变
5楼-- · 2019-01-13 17:03
  1. The meta tag must go before all PrimeFaces stuff: http://blogs.msdn.com/b/cjacks/archive/2012/02/29/using-x-ua-compatible-to-create-durable-enterprise-web-applications.aspx

  2. HTTP Header and HTML HEAD are completly different things.

  3. In PrimeFaces 3.0 the new facet was added to h:head: http://blog.primefaces.org/?p=1433 So the solution would be:

<h:head>
    <f:facet name="first">
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
    </f:facet>
</h:head>
查看更多
Ridiculous、
6楼-- · 2019-01-13 17:03

I think best solution is to create JSF PhaseListener which adds X-UA-Compatible header to HTTP response

public class UACompatibleHeaderPhaseListener implements PhaseListener {
    private static final long serialVersionUID = 1L;

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        final FacesContext facesContext = event.getFacesContext();
        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.addHeader("X-UA-Compatible", "IE=edge");
    }

    @Override
    public void afterPhase(PhaseEvent event) {
    }

}

and register it in faces-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" version="2.0">
  <lifecycle>
    <phase-listener>com.example.UACompatibleHeaderPhaseListener</phase-listener>
  </lifecycle>
</faces-config>

Another option would be to create servlet Filter and register it in web.xml.

Why is this needed?

  1. Imagine your web application is deployed on a domain (or sub-domain) which is on IE compatibility list here: http://ie9cvlist.ie.microsoft.com/ie9CompatViewList.xml so you need to use X-UA-Compatible header to switch IE back to latest mode.

  2. Imagine your web application is deployed on WebLogic server (which uses mojarra 2.0.4) so you cannot change JSF implementation.

查看更多
登录 后发表回答