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.

4

6 回答 6

1

使用 javascript 使用导航器

 navigator.userAgent.indexOf('MSIE')

现在,如果你真的想要 IE 和版本,你应该从 msdn 检查这个脚本

更有效地检测 Windows Internet Explorer

于 2013-07-02T14:46:49.697 回答
0

您是否尝试过查看“navigator.userAgent”。您将能够很简单地判断它的IE。还有其他有用的浏览器检测脚本,但如果您只需要对 IE 进行简单检查,我会坚持使用上述方法。

于 2013-07-02T14:44:47.380 回答
0

这是一个使用 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>
于 2013-07-02T14:51:07.093 回答
0

另一种方法是使用条件注释,比如 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');
  } 
});

当您将其与modernizr结合使用时,您将获得 UA 嗅探和全特征检测的“好处”。

于 2013-07-02T15:00:38.653 回答
0

JavaScript 中的条件注释,无需借助用户代理嗅探:

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');
于 2013-07-02T15:00:50.693 回答
0

您可以使用以下功能进行检查

(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;})();

但是您可以使用更多选项。看看http://www.jquery4u.com/browsers-2/check-ie-version/

于 2013-07-02T14:51:11.933 回答