Internet Explorer Detection : The power of conditional comments but via jQuery
by Developer @ pebbl.co.uk on Wednesday, 24 April 2013
I came across a situation the other day where I needed a specific browser check (IE7 or less), there wasn't a particular testable feature to hand that would do and I didn't have access to the markup. My usual fix for Internet Explorer specific bugs is to use conditional comments, they do exactly what it says on the tin, however.. not having access to the page markup thwarted this otherwise quick and easy solution.
Rather than falling back to tenuous agent string checking — particularly worrying when it comes to specific version number checks — or bashing my head against multiple feature tests that could be invalidated in the future, I thought the following implementation would be no less mad, and maybe even slightly useful:
var isOldIE = function(query){
return $('<!--['+(query||'if lte IE 7')+']>OLDIE<![endif]-->')
.text() === 'OLDIE'
;
}
Make of it what you will, but I rather like it... even though it is hideous from an optimisation point of view.
- Leave your comment • Category: Coding, JavaScript
- Share on Twitter, Facebook, Delicious, Digg, Reddit