1

I am trying to run a JQuery function based on whether or not the user is running IE. The reason is because the function that I have does not work in IE, so I have another one I would like to run instead when the user is using IE. I am running JQuery 1.9.1, so I cannot use $.browser. I'm trying to use conditional comments, but I'm not sure if that is the way to go. I've tried to implement it here, but it doesn't work: http://jsfiddle.net/AsQ4E/

This is the HTML

<!--[if IE]>
    <input type="button" onclick="javascript:ie();" value="Click Me!" />
<![endif]-->

<!--[if !IE]> -->
    <input type="button" onclick="javascript:notIE();" value="Click Me!" />
<!-- <![endif]-->

This is the JQuery

function ie() {
    alert("You are using IE");
}

function notIE() {
    alert("You are not using IE");
}

How can I get this to work? Is this the best way to go about it?


EDIT: I am trying to use SVG Crowbar. When the user clicks a button, Crowbar extracts an SVG from the page and pops up a download for it. Here is the link to it: http://nytimes.github.io/svg-crowbar/. The problem is is that it doesn't work in IE, so I was going to use this only when the user is using IE: http://bl.ocks.org/biovisualize/1209499 (This works in IE). I don't know enough about programming and Javascript to debug the problem with IE.

TFischer
  • 1,278
  • 2
  • 24
  • 42
  • Your code works fine. You just need to check the settings on the fiddle, the defaults aren't correct. Your functions need to be in the `` for the buttons to work (change `onLoad` to `No wrap - in `). Also if you are using jQuery, try to bind events *correctly*. Meaning, don't use `onclick` attributes. – gen_Eric Jul 02 '13 at 14:44
  • 2
    Show us your JavaScript function that doesn't work in IE. Also, Conditional Comments won't work in IE10. – dsgriffin Jul 02 '13 at 14:44
  • 1
    What is the function that doesn't work in IE? Why doesn't it work? What doesn't work about it? Usually checking for features is better than checking for browsers. So, there is probably a better solution here than checking for IE! – gen_Eric Jul 02 '13 at 14:47
  • I updated the question with more information – TFischer Jul 02 '13 at 14:57
  • Check out my answer here: http://stackoverflow.com/questions/14892095/browser-msie-error-after-update-to-jquery-1-9-1/14892171#14892171 It uses conditional comments in javascript. – Johan Jul 02 '13 at 14:58

6 Answers6

1

With javascript using navigator

 navigator.userAgent.indexOf('MSIE')

Now if you really want the IE and the version you should check this script from the msdn

Detecting Windows Internet Explorer More Effectively

Jorge
  • 17,896
  • 19
  • 80
  • 126
  • 1
    Thank God it will not work on IE 11 http://www.engadget.com/2013/03/25/ie-11-says-it-is-like-firefox/ – Pavlo Jul 02 '13 at 14:49
  • 1
    Nice information on that msdn link Jorge watch out for using indexOf with IE below IE9 – jjay225 Jul 02 '13 at 14:50
0

Have you tried looking at "navigator.userAgent". You'll be able to tell if its IE quite simply with that. There are other useful browser detection scripts but if you just need a simple check for IE I'd stick with the above.

jjay225
  • 508
  • 6
  • 12
0

Here is an example using a function propopsed by msdn

http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function getInternetExplorerVersion()
            // Returns the version of Internet Explorer or a -1
            // (indicating the use of another browser).
        {
            var rv = -1; // Return value assumes failure.
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null)
                    rv = parseFloat(RegExp.$1);
            }
            return rv;
        }

        function myFunction() {
            if (getInternetExplorerVersion() > -1)
                ie();
            else
                notIE();
        }

        function ie() {
            alert("You are using IE");
        }

        function notIE() {
            alert("You are not using IE");
        }
    </script>
</head>
<body>
    <input type="button" onclick="javascript:myFunction();" value="Click Me!" />
</body>
</html>
Eric Beaulieu
  • 1,054
  • 7
  • 11
0

You can use following function to check

(function() {
  "use strict";
  var tmp = (document["documentMode"] || document.attachEvent) && "ev"
       , msie = tmp 
                  && (tmp = window[tmp + "al"])
                  && tmp("/*@cc_on 1;@*/")
                  && +((/msie (\d+)/i.exec(navigator.userAgent) || [])[1] || 0)
  ;
  return msie || void 0;})();

But ther are more options which you can use. Look at http://www.jquery4u.com/browsers-2/check-ie-version/

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
0

An alternative would be to use conditional comments, ala the HTML5 Boilerpalte:

HTML

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->

JS

// tweak depending on version of IE
var isIe = $('.lt-ie9').length > 0; // or tagName / document.body etc...

button.on('click', function () {
  if (isIe) {
   console.log('ie');
  }
  else {
   console.log('not ie');
  } 
});

When you combine this with modernizr, you get the "benefits" of UA sniffing with full feature detection.

Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
0

Conditional comments in JavaScript, without resorting to user agent sniffing:

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());

if(ie)
    alert('You are using IE version' + ie);
else
    alert('You are not using IE');
Johan
  • 35,120
  • 54
  • 178
  • 293