简体   繁体   English

如何在JAVA中获取Internet Explorer的文档模式

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

I am trying to fetch the document mode of the browser ie IE from the given function.I knew that in javascript we can use document.documentMode to fetch the doucment mode of IE. 我试图从给定的功能中获取浏览器的文档模式,即IE。我知道在javascript中我们可以使用document.documentMode来获取IE的document.documentMode模式。 But is there any way to do it in java ? 但是在Java中有什么方法吗? I have the userAgent string fro the HttpServletRequest ,but I cannot use it to fetch the document mode.I have used ScriptEngine to execute the javascript inside the java code ,but It is giving exception that the document element is not defined.Kindly help please 我有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();
    }

What you are doing here executes JavaScript on the server side. 您在此处执行的操作是在服务器端执行JavaScript。 What you need is JavaScript on the client's browser. 您需要的是客户端浏览器上的JavaScript。 One way to achieve what you want is to pass on the documentMode as a URL param. 实现所需目标的一种方法是将documentMode作为URL参数传递。 This will be available on the server. 这将在服务器上可用。

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

Now the URL param documentMode will be available with request.getParameter("documentMode") 现在,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