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