簡體   English   中英

如何在JAVA中獲取Internet Explorer的文檔模式

[英]How to fetch the document mode of Internet Explorer in JAVA

我試圖從給定的功能中獲取瀏覽器的文檔模式,即IE。我知道在javascript中我們可以使用document.documentMode來獲取IE的document.documentMode模式。 但是在Java中有什么方法嗎? 我有HttpServletRequestuserAgent字符串,但是我不能用它來獲取文檔模式。我已經使用ScriptEngine在Java代碼中執行javascript了,但是文檔元素未定義是一個例外。請幫忙

 ScriptEngine engine = 
            new ScriptEngineManager().getEngineByName("javascript");
    String docversio = null;
    String script = "function documentversion() { return document.documentMode }";
    try {
         engine.eval(script);

         Invocable inv = (Invocable)engine;

        try {
            docversio = (String) inv.invokeFunction("documentversion");
        } catch (NoSuchMethodException e) {
            System.out.println("No such method");
            e.printStackTrace();
        } 

        if(null != docversio)
        System.out.println("the document version is "+docversio);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您在此處執行的操作是在服務器端執行JavaScript。 您需要的是客戶端瀏覽器上的JavaScript。 實現所需目標的一種方法是將documentMode作為URL參數傳遞。 這將在服務器上可用。

<script>
$(document).ready(function(){
if ( document.referrer == null || document.referrer.indexOf(window.location.hostname) < 0 ) {
   window.location.href = window.location.href + "?documentMode=" + document.documentMode;
}
});
</script>

現在,URL參數documentMode將與request.getParameter("documentMode")

public class DocumentModeOfIE extends HttpServlet {
  private String documentMode;

  public void init(ServletConfig config) throws ServletException {    }

  public void doGet(HttpServletRequest req, HttpServletResponse resp) 
       throws ServletException, IOException {
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/html");
    documentMode = req.getHeader("X-UA-Compatible");
    out.println(documentMode);
  }

  public void destroy() {  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM