运行JQuery的功能时,浏览器是IE(Run JQuery function when brows

2019-10-18 07:43发布

我试图运行基于用户是否正在运行IE jQuery的功能。 究其原因是因为我具备的功能并不在IE浏览器,所以我有另外一个我想,当用户使用IE浏览器,而不是运行。 我运行的JQuery 1.9.1,所以我不能使用$.browser 。 我试图使用条件的意见,但我不知道这是要走的路。 我试图在这里实现它,但它不工作: http://jsfiddle.net/AsQ4E/

这是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]-->

这是JQuery的

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

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

我怎样才能得到这个工作? 这是为了去了解它的最佳方式?


编辑:我想使用SVG撬棍。 当用户点击一个按钮,撬棒从页面中提取的SVG和弹出一个下载它。 这里是链接到它: http://nytimes.github.io/svg-crowbar/ 。 问题是在于,它并不在IE工作,所以我打算只有当用户使用IE浏览器使用此: http://bl.ocks.org/biovisualize/1209499 (这在IE)。 我不知道有足够的了解编程和Javascript的IE调试问题。

Answer 1:

随着使用JavaScript导航

 navigator.userAgent.indexOf('MSIE')

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

检测的Windows Internet Explorer更有效



Answer 2:

您是否尝试过寻找“navigator.userAgent的”。 您可以如果IE告诉很简单这一点。 还有其他有用的浏览器检测脚本,但如果你只需要一个简单的检查IE浏览器我上述坚守。



Answer 3:

这里是使用由MSDN propopsed的功能的示例

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>


Answer 4:

您可以使用下面的函数来检查

(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/



Answer 5:

另一种方法是使用条件注释,阿拉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的“好处”与全功能检测嗅探。



Answer 6:

在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');


文章来源: Run JQuery function when browser is IE