views:

139

answers:

5

Hi Guys,

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

Edit: Use conditional comments

<!--[if !IE]>
<!--[if lt IE 8]> 

//do stuff
<![endif]-->
<![endif]-->
+6  A: 

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.

meder
I am trying to do - if the #element is ONLY on IE7 then leave the CSS as display:none; - otherwise in every other browser change it. This is the because the CSS renders first and the slider I am using it in is too slow to rely on JS.
Tom
You can use CSS and IE Conditional statements ( not JS ).
meder
yeah but i'd prefer not to use conditional statements as Google Pagespeed doesnt like that.
Tom
You shouldn't let an application like that tell you what you can or can't do, for practical solutions. Google shouldn't really be one telling to what code is good or not, look at gmail.. HORRIBLE coding job.
meder
@Tom - Pagespeed is wrong in this case...if it had a check for this, it would recommend conditional stylesheets over javascript/user agent detection.
Nick Craver
ah right ok. well, it confused the crap out of me so this is why i went looking for jquery.browser.
Tom
A: 

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.)
  }
});
Adam
hey thx. but wont this only work if MSIE - i.e. its testing for MSIE and then do stuff. I need every OTHER browser but <8 ?
Tom
@Tom I'm not sure what you are trying to do so I modified my code to show where to put code for each situation you may be trying to deal with.
Adam
+1  A: 

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'))...
DA
+2  A: 

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.

Nick Craver
A: 
jQuery.browser.msie

returns true or false

jQuery.browser.version

is the one to check browserversion.

Morgen32