我试图从给定的函数中获取浏览器的文档模式,即 IE。我知道在 javascript 中我们可以使用 document.documentMode
来获取 IE 的文档模式。但是有什么方法可以在java中做到这一点吗?我有来自 HttpServletRequest
的 userAgent
字符串,但我无法使用它来获取文档模式。我已经使用 ScriptEngine
来执行里面的 javascript java代码,但它给出了文档元素未定义的异常。请帮忙
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")
关于java - 如何在JAVA中获取Internet Explorer的文档模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043942/