简体   繁体   English

为 IE9 (GWT) 设置用户代理

[英]Set user agent for IE9 (GWT)

I'm trying to change the user agent to safari when the page is visited using IE9.当使用 IE9 访问页面时,我正在尝试将用户代理更改为 safari。

I wrote this to the gwt.xml file:我将此写入 gwt.xml 文件:

<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).然而 IE9 现在根本找不到任何用户代理(甚至不回退到它自己的)。 What am I doing wrong?我究竟做错了什么?

EDIT: I have to use GWT 2.0.4编辑:我必须使用 GWT 2.0.4

EDIT2: Read comments of answer to see how I fixed it. EDIT2:阅读答案的评论,看看我是如何修复它的。

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,).我以前没有这样做过,但我假设您必须实现自己的 UserAgentPropertyGenerator(因为这是动态评估用户代理属性的 class)。 and then bind it like in com.google.gwt.user.UserAgent.gwt:xml:然后像在 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: 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.我不是 100% 确定我理解你的问题。 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.您可能想要做的是处理来自 ie9 浏览器的请求,就好像它们来自 safari 浏览器一样。 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:在这种情况下,我认为您可以使用使用替换的延迟绑定:此 web 页面中的示例对您来说看起来像这样:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM