如何在JAVA中获取internetexplorer的文档模式
我正在尝试从给定函数获取浏览器的文档模式,即IE。我知道在javascript中,我们可以使用如何在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
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() { }
}