开发者

Set user agent for IE9 (GWT)

I'm trying to change the user agent to safari when the page is visited using IE9.

I wrote this to开发者_运维知识库 the gwt.xml file:

<set-property name='user.agent' value='safari'>
   <when-property-is name="user.agent" value="ie9" />
</set-property>

However IE9 now doesn't find any user agent at all (doesn't even fall back to its own). What am I doing wrong?

EDIT: I have to use GWT 2.0.4

EDIT2: Read comments of answer to see how I fixed it.


I haven't done it before, but I assume you will have to implement your own UserAgentPropertyGenerator (because this is the class that evaluates the user agent property dynamically!), and then bind it like in com.google.gwt.user.UserAgent.gwt.xml:

<property-provider 
   name="user.agent"
   generator="com.google.gwt.user.rebind.UserAgentPropertyGenerator"/>

For older versions of GWT (< 2.3.0), this is configured directly in com.google.gwt.user.UserAgent.gwt.xml as:

  <property-provider name="user.agent"><![CDATA[
      var ua = navigator.userAgent.toLowerCase();
      var makeVersion = function(result) {
        return (parseInt(result[1]) * 1000) + parseInt(result[2]);
      };

      if (ua.indexOf("opera") != -1) {
        return "opera";
      } else if (ua.indexOf("webkit") != -1) {
        return "safari";
      } else if (ua.indexOf("msie") != -1) {
        if (document.documentMode >= 8) {
          return "ie8";
        } else {
          var result = /msie ([0-9]+)\.([0-9]+)/.exec(ua);
          if (result && result.length == 3) {
            var v = makeVersion(result);
            if (v >= 6000) {
              return "ie6";
            }
          }
        }
      } else if (ua.indexOf("gecko") != -1) {
        var result = /rv:([0-9]+)\.([0-9]+)/.exec(ua);
        if (result && result.length == 3) {
          if (makeVersion(result) >= 1008)
            return "gecko1_8";
          }
        return "gecko";
      }
      return "unknown";
  ]]></property-provider>


I'm not 100% sure that I understand your question. However, I believe you can't really change the user agent. What you probably want to do is handling requests from ie9 browser as if they were coming from safari browsers. In this case I think you can use Deferred Binding Using Replacement: the example in this web page would look something like this for you:

<module>

  <!--  ... other configuration omitted ... -->

  <!-- Fall through to this rule is the browser isn't IE or Mozilla -->
  <replace-with class="com.google.gwt.user.client.ui.impl.PopupImpl">
    <when-type-is class="com.google.gwt.user.client.ui.impl.PopupImpl"/>
  </replace-with>

  <!-- Handling safari and ie9 in the same way -->
  <replace-with class="com.google.gwt.user.client.ui.impl.PopupImplSafari">
    <when-type-is class="com.google.gwt.user.client.ui.impl.PopupImpl" />
    <any>
      <when-property-is name="user.agent" value="safari"/>
      <when-property-is name="user.agent" value="ie9" />
    </any>
  </replace-with>

</module>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜