I am trying to use
jQuery(document).ready(function () {
if (!jQuery.browser.msie <= 7.99) {
jQuery('#element').css('display', 'block');
}
});
But it doesn't appear to work ? What am I doing wrong ?
Thx
开发者_JAVA技巧Edit: Use conditional comments
<!--[if !IE]>
<!--[if lt IE 8]>
//do stuff
<![endif]-->
<![endif]-->
jQuery.browser.version
is what you're looking for. And you should probably parseFloat
it.
In general though, it's looked down upon to rely on browser sniffing unless there's absolutely no way to feature detect. It might help telling us what your real problem is.
EDIT: You should use conditional comments to serve rules/stylesheet(s) for IE7 and below.
This seems to work from my limited testing:
jQuery(document).ready(function () {
if(jQuery.browser.msie){
if(parseFloat(jQuery.browser.version) < 8){
//Versions of IE less than 8
jQuery('#element').css('display', 'block');
}
else{
//code for versions of IE at least 8 (currently 8 and 9)
}
}
else{
//code for browsers other than IE (Firefox Safari, chrome, Opera, etc.)
}
});
This doesn't answer your specific question, but I'd propose a much simpler methodology. Use IE conditional comments to apply a specific ID to your body tag. For instance:
<!--[if !IE]> -->
<body>
<!--<![endif]-->
<!--[if IE 7]>
<body id="IE7">
<![endif]-->
Then it becomes quite trivial to detect it via jQuery:
if($('body').is('#IE7'))...
You should use conditional comments for this, quicker, easier, shorter, like this:
<!--[if lt IE 8]>
<style type="text/css">#element { display: none; }</style>
<![endif]-->
This will hide the element on IE7 and below. You don't need any script to go along with this, just remove the display: none
you currently have hiding it initially from your original stylesheet (or in-line).
For the comments concerning Google Pagespeed not liking this...ignore it, if you have to fix an IE7 bug, fix it, the right way. This is faster and simpler...if Pagespeed was able to check that you're using the user agent to do this (which jQuery.browser
does) it would recommend against doing so, it just doesn't have a mechanism to tell you that's a worse approach.
jQuery.browser.msie
returns true or false
jQuery.browser.version
is the one to check browserversion.
You can use the jQuery Browser Plugin. It gives you a javascript object that contains all of the information about the browser being used.
For example:
browser.name
: The name of the browser being used.
alert($.browser.name); // this will alert 'firefox'
- browser.versionNumber`` : The version of the browser (note: as an integer).
alert($.browser.versionNumber); // this will alert '30'
I personally found it really simple to use. The minified version is only 1.44KB. Please refer to the link.
精彩评论