HI all ,
I am trying to fetch the browser version as well as document mode of the browser i.e IE from the given function.I knew that in javascript we can use document.documentMode to fetch the doucment mode of IE.but is there any way to do it in 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
private float getIEVersion(String useragent) { 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(); } float ieVersion = -1; if (useragent != null && useragent.length() > 0) { useragent = useragent.toLowerCase(); if (useragent.contains("msie")) { Pattern iePattern = Pattern.compile("msie ([0-9]{1,}[\\.0-9]{0,})"); Matcher m = iePattern.matcher(useragent); if (m.find()) ieVersion = Float.parseFloat(m.group(1)); } } //System.out.println("The ie version is "+ ieVersion); return ieVersion; }