1. 程式人生 > 實用技巧 >IE5遷移到IE11過程中遇到的一些問題

IE5遷移到IE11過程中遇到的一些問題

IE5遷移到IE11過程中遇到的一些問題

  1. document.getElementById("idName")

    IE11中嚴格區分idName的大小寫,IE5不區分大小寫,如果你在適配過程中遇到瀏覽器控制檯報null錯誤, 就應該檢查報錯行是否idName大小寫錯誤

  2. 獲取frame的寫法變化

    • IE5寫法: document.frames("frameName")

    • IE11寫法:document.frames["frameName"]

  3. 使用xhr(XMLHttpRequest)時, url不會自動將特殊字元轉義,而是在特殊字元處截斷, 需手動轉義, 測試程式碼如下

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>test xhr</title>
    </head>
    <body>
    
    <p>點選按鈕執行 <em>test()</em> 函式.</p>
    <button onclick="test()">點這裡</button>
    
    <script>
        function test() {
            var xhr;
            if (window.ActiveXObject) {
                alert("正在使用舊版本的Internet Explorer(IE5和IE6)")
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                xhr = new XMLHttpRequest();
            }
            var url = "http://localhost:8080/awesome/clientIp?param1=#test#";
            // 在ie5中發現: 實際執行的url已被轉義為:http://localhost:8080/awesome/clientIp?param1=%23test%23
            // 在ie11和chrome中發現: 實際執行的url已被截斷為:http://localhost:8080/awesome/clientIp?param1=
            xhr.open('GET', url, false);
            xhr.send(null);
        }
    </script>
    
    </body>
    </html>
    
  4. IE11中微軟棄用了zh_CN語言,而採用 zh_hans_CN,造成前臺頁面不能正常國際化, 後端可增加國際化過濾器解決此問題

    • 後端測試介面 程式碼

          // curl -v localhost:8080/awesome/locale
          // ie開啟:http://localhost:8080/awesome/locale ,請求頭:Accept-Language: zh-Hans-CN, zh-Hans; q=0.5 ,後端列印 zh_CN_#Hans
          // chrome開啟: http://localhost:8080/awesome/locale ,請求頭: Accept-Language: zh-CN,zh;q=0.9 ,後端列印 zh_CN
          @GetMapping("/locale")
          public Locale locale(HttpServletRequest httpServletRequest) {
              Locale locale = httpServletRequest.getLocale();
              logger.info("/locale , locale is:{}", locale);
              return locale;
          }
      
    • 國際化過濾器程式碼

      package com.varyuan.awesome.filter;
      
      import org.springframework.stereotype.Component;
      
      import javax.servlet.*;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletRequestWrapper;
      import java.io.IOException;
      import java.util.Locale;
      
      // 國際化過濾器
      // 適配情況:ie11中棄用了zh_CN請求頭,而採用zh-Hans-CN
      @Component
      public class I18nFilter implements Filter {
      
          private final String zh = Locale.CHINA.getLanguage();
          private final String CN = Locale.CHINA.getCountry();
      
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
              HttpServletRequestWrapper zhCnWrapper = new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {
                  @Override
                  public Locale getLocale() {
                      Locale loc = super.getLocale();
                      String language = loc.getLanguage();
                      String country = loc.getCountry();
                      if ((zh.equals(language) && CN.equals(country))) {
                          return Locale.CHINA;
                      } else {
                          return loc;
                      }
                  }
              };
              filterChain.doFilter(zhCnWrapper, servletResponse);
          }
      }