0

I want to check the browser is IE or not using jquery. If the browser is IE change some class.I tried something like

$(document).ready(function () {
    <![if !IE]>
   ---------------
    <![endif]>
    });

But it didn't return my needs .anyone help?

Spudley
  • 166,037
  • 39
  • 233
  • 307
Arun
  • 1,402
  • 10
  • 32
  • 59
  • Use navigator.userAgent – Dineshkani Jun 03 '13 at 10:00
  • 5
    Why would you want to do this? There's almost never a good reason to check which browser in in use, and it seems unlikely that you've stumbled upon one of those reasons if you don't actually know how to do it. What's the _real_ problem you're trying to solve? – nnnnnn Jun 03 '13 at 10:08
  • Why do you need to do this? One of the main reasons for using jQuery is to cut out the need for browser-specific code. And even when you do need to do something specific depending on the browser, it's generally better to use feature detection rather than browser detection. In other words, there is almost certain to be a better solution to your problem than trying to check for IE. – Spudley Jun 03 '13 at 10:08
  • possible duplicate of [What's the replacement for $.browser](http://stackoverflow.com/questions/9645803/whats-the-replacement-for-browser) – Quentin Jun 03 '13 at 10:09

8 Answers8

3

You haven't specified enough about what your actual problem is, but I guess you're trying to cope with a feature that is missing in (some versions of) IE.

The main thing I would say is that your approach to solving the problem is fundamentally wrong.

For a start, the capabilities of IE (as for any other browser) vary wildly between the various versions. IE10 is actually pretty good, and supports most of the modern browser features you might want to use. And IE11 will be released in the near future with even better support. You might have a problem with IE8 or earlier, but most well-written code ought to work with IE10 without any problems. Therefore, a blanket check for IE without checking the version is almost certainly a bad idea.

Secondly, even if you are going to do an IE check, <![if !IE]> is wrong because IE has dropped support for this since IE10. The reason they've dropped it is specifically to discourage the bad practice of browser detection.

There are various other ways of detecting IE, but they're all bad practice for the same reason: detecting the browser and making your site work differently for different browsers has a whole load of issues with it. It's a big topic, so I suggest reading here for more info.

Finally, what to do instead? The answer is feature detection.

In short:

  1. Work out what feature it is that you need that isn't supported in older browsers
  2. Write a script that detects whether the user's browser supports that feature.
  3. Write your code to cope with a negative answer from that check.
    (this may include 'polyfill' scripts that add the missing features to the browser, or some kind of alternative functionality for unsupported browsers)

This technique allows you to write your site in a way that deals with all browsers regardless of what capabilities they have.

A good library you should try for feature detection is Modernizr.

ps - If you're having trouble with something specific in IE, you should ask a separate question about it; there's a good chance that the folk here will be able to help you get it working, and the whole 'how do I detect IE' question would be unnecessary anyway.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Actually my need is to check whether the browser is IE or not.if the browser is Ie make some changes in css.thats only – Arun Jun 03 '13 at 11:13
  • @Arun You can also write css hacks specified to IE: A small collection is present here: https://github.com/logeshpaul/CSS-Hacks#4-attribute-hacks- – Andreas Louv Jun 03 '13 at 11:16
  • @NULL how css hack help me for my need? – Arun Jun 03 '13 at 11:19
  • @Arun As you said in this comment thread: `if the browser is Ie make some changes in css.thats only`?? I understand it as: If the browser is IE then I would like to make some changes in the css. Thats the only thing I want. – Andreas Louv Jun 03 '13 at 11:22
  • 1
    @Arun - what specific CSS changes do you need to make? CSS should generally work the same in IE as any other browser. Are you sure your site isn't doing something like running IE in quirks mode? That would indeed make IE's CSS work differently, but the solution to that is to have a valid doctype for your page, and definitely **not** to write different CSS for IE. – Spudley Jun 03 '13 at 11:38
1

$.browser is deprecated and shouldn't be used.

You can use feature detection instead to check for specific things you need to render your page. See the jQuery docs here: http://api.jquery.com/jQuery.support/

John H
  • 2,488
  • 1
  • 21
  • 35
1

There is a blog post about this telling you shouldn't use userAgent. Very interesting read:

http://tanalin.com/en/articles/ie-version-js/

Some code snippets:

if (document.all && !document.querySelector) {
    alert('IE7 or lower');
}
if (document.all && document.querySelector && !document.addEventListener) {
    alert('IE8');
}
if (document.all && document.documentMode && 8 === document.documentMode) {
    alert('IE8 or IE9+ in IE8 compatibility mode');
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • 1
    To be honest, I wouldn't rely on this stuff either. This is taking the good idea of feature detection, and turning it into an even worse way of doing browser detection. The correct way to do feature detection is to detect for the features that you are actually using, not for features that you think are unique to a particular browser. – Spudley Jun 03 '13 at 11:05
  • 1
    @Spudley I wouldn't argue against that but sometimes the problem can be that confusing that you just need at dirty IE fix, and you aren't sure what feature you should check against. But sure enough you could also load a "framework" like modernizr and that way know about all the features (not) available to you. – Andreas Louv Jun 03 '13 at 11:13
  • most of the time if the problem is *that* confusing, it indicates a deeper issue that needs to be fixed. Working around it with an IE fix (dirty or otherwise) rather than finding the root cause may actually make things worse in the long run. (eg in this case, from the comments on my answer, I'm begining to suspect he may be in Quirks mode; that can be fixed very easily by adding a doctype. Trying to fix it by tweaking the CSS in IE may get things moving in the right direction, but will still leave holes that can only really be fixed by adding a doctype) – Spudley Jun 03 '13 at 11:44
0

My bad, I didn't check but jQuery.browser is deprecated as of jQuery version 1.9.

Instead the correct practice using jQuery would involve feature detection with jQuery.support()

Description: A collection of properties that represent the presence of different browser features or bugs. Primarily intended for jQuery's internal use; specific properties may be removed when they are no longer needed internally to improve page startup performance.

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
0

you can try this, without the need of jQuery, but with conditional comments:

<!doctype html>
<!--[if IE 8 ]><html lang="en" class="no-js ie8"><![endif]-->
<!--[if IE 9 ]><html lang="en" class="no-js ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<html lang="en" class="no-js">
<!--<![endif]-->
Mark
  • 6,762
  • 1
  • 33
  • 50
  • 1
    Bear in mind that IE10 (and future IE versions) no longer support conditional comments, so this technique won't work for detecting later IE versions. (it's been removed explicitly to discourage the bad practice of browser detection) – Spudley Jun 03 '13 at 11:07
0

jQuery.browser was deprecated in jQuery 1.9.

You should try jQuery Migrate - https://github.com/jquery/jquery-migrate

Similar question to - Easiest/Lightest Replacement For Browser Detection jQuery 1.9?

Community
  • 1
  • 1
Alex
  • 8,875
  • 2
  • 27
  • 44
0

Try this to check browser.

 if ( $.browser.msie ) {
     alert( $.browser.version );
  }

The $.browser property provides information about the web browser that is accessing the page, as reported by the browser itself. It contains flags for each of the four most prevalent browser classes (Internet Explorer, Mozilla, Webkit, and Opera) as well as version information.

Available flags are:

  • webkit (as of jQuery 1.4)
  • safari (deprecated)
  • opera
  • msie
  • mozilla

Reference

Amit
  • 15,217
  • 8
  • 46
  • 68
0

I guess you can still use conditional comments to check the whether browser is IE or not... Please go thru the blog posted here

 <!--[if IE]> 
     This content is ignored in IE10 and other browsers.    
     In older versions of IE it renders as part of the page.    
<![endif]-->

====================================================================
    <!--[if lt IE 9]>
       <script src="jquery-1.9.1.js"></script>
    <![endif]-->
       //There is difference in the commenting style which jQuery team has used!
    <!--[if gte IE 9]><!-->
        <script src="jquery-2.0.0b2.js"></script>
    <!--<![endif]-->
John Smith
  • 1,095
  • 2
  • 11
  • 24