JSTL Agent identification
So converting NT2.2 to be IE6 friendly was a kick in the teeth... and as Dustin posted earlier, it would have been nice to throw up a warning window and let them see the horror or an IE6 rendering job. However we coded it up to be usable in IE6 and in the process had to rewrite some of the homepage. At first I used the ugly <![if IE 6]> // do something magical <![endif]--> tags, but they caused Safari to explode on itself. So I went back did a quick study and came out with a JSTL solution, and then modded it with Drew to get it working.
Using JSTL you can get the browser through the user-agent string in the URL request header. Like this:
<c:set var="browser" value="${header['User-Agent']}" scope="page"/> |
You can see that I used a c:set to get the 'User-Agent' string and set the variable 'browser' to that value.
Now we just need to set up a c:choose to determine the path of IE 6 or other browser types using the indexOf function in the JSTL 'fn' library. So a quick c:choose statement would look like this.
<c:set var="browser" value="${header['User-Agent']}" scope="page"/> <c:choose> <c:when test="${!empty browser && fn:indexOf(browser, 'MSIE 6') >= 0}"> NOT EMPTY </c:when> <c:otherwise> EMPTY </c:otherwise> </c:choose> |
In the 'c:when test' you can see that the indexOf ends with a '>=0', this is because indexOf returns an INT, which is treated Boolean-esque in this case (it's not a Boolean, but the way we are using it resembles a Boolean).
Don't forget that when using the JSTL fn and c tag libraries you need to include them in your jsp file. use the following at the top of your page to use these excellent tag-libs.
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> |
If you have any questions, leave'em in the comments
.
Print This Post