Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JAVA中获取internetexplorer的文档模式_Java_Javascript_Internet Explorer - Fatal编程技术网

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

如何在JAVA中获取internetexplorer的文档模式,java,javascript,internet-explorer,Java,Javascript,Internet Explorer,我正在尝试从给定函数获取浏览器的文档模式,即IE。我知道在javascript中,我们可以使用document.documentMode获取IE的doucment模式。但是有没有办法在java中实现呢?我有HttpServletRequest中的userAgent字符串,但我无法使用它来获取文档模式。我使用ScriptEngine在java代码中执行javascript,但它给出了文档元素未定义的异常。请帮助 ScriptEngine engine = new Scr

我正在尝试从给定函数获取浏览器的文档模式,即IE。我知道在javascript中,我们可以使用
document.documentMode
获取IE的doucment模式。但是有没有办法在java中实现呢?我有
HttpServletRequest
中的
userAgent
字符串,但我无法使用它来获取文档模式。我使用
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>

$(文档).ready(函数(){
if(document.referer==null | | document.referer.indexOf(window.location.hostname)<0){
window.location.href=window.location.href+“?documentMode=“+document.documentMode;
}
});

现在URL参数documentMode将可用于
request.getParameter(“documentMode”)

这是servlet代码,不是吗?所以它在服务器上运行?是的,并且这个代码片段在一个从doGET()调用的方法中。但是当它在服务器上运行时,您怎么能期望它会改变客户端上的某些内容呢?
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() {  }

}