我通过单击具有特定类的divs调用如下函数。

是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。

如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。

示例函数:

$('.myClass').on('click', function(event)
{
    // my function
});

当前回答

Necromancing。

为了不依赖于user-agent字符串,只需检查以下几个属性:

if (document.documentMode) 
{
    console.log('Hello Microsoft IE User!');
}

if (!document.documentMode && window.msWriteProfilerMark) {
    console.log('Hello Microsoft Edge User!');
}

if (document.documentMode || window.msWriteProfilerMark) 
{
    console.log('Hello Microsoft User!');
}

if (window.msWriteProfilerMark) 
{
    console.log('Hello Microsoft User in fewer characters!');
}

此外,这将检测新的Chredge/Edgium(阿纳海姆):

function isEdg()
{ 

    for (var i = 0, u="Microsoft", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}

这个可以检测铬:

function isChromium()
{ 

    for (var i = 0, u="Chromium", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}

还有这个Safari:

if(window.safari)
{
    console.log("Safari, yeah!");
}

其他回答

我知道这是一个老问题,但如果有人再次遇到这个问题,并且无法检测IE11,这里有一个适用于所有当前版本的IE的有效解决方案。

var isIE = false;
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
    isIE = true;   
}

我只是想检查一下浏览器是否是IE11或更老的版本,因为它们都是垃圾。

function isCrappyIE() {
    var ua = window.navigator.userAgent;
    var crappyIE = false;
    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {// IE 10 or older => return version number        
        crappyIE = true;
    }
    var trident = ua.indexOf('Trident/');
    if (trident > 0) {// IE 11 => return version number        
        crappyIE = true;
    }
    return crappyIE;
}   

if(!isCrappyIE()){console.table('not a crappy browser);}

这是另一种不用查看User-Agent就能检测IE的方法:

var usingIE="__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR"在文档中; alert("You are"+(use ingie ?"":"n't")+" using ie .");

我在测试我的网站是否能在IE上运行时偶然发现了这个,我打开调试器,点击文件夹图标。里面有我的脚本,还有一个我没有的动态脚本文件夹。我打开它,发现有很多browsertools.library.js文件。在它们里面,我发现了这样的东西:

document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = undefined;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = false;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = undefined;
try{
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eval("\r\n//# sourceURL=browsertools://browsertools.library.js");
}
catch( eObj ){
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = eObj.number;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eObj.message || eObj.description || eObj.toString();
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = true;
}

所以我用这些来测试用户的浏览器是否是IE。请注意,这只适用于你想知道他们是否有IE,而不是确切地知道他们有什么版本的IE。

这里有很多答案,我想补充我的意见。IE 11在flexbox上是如此的麻烦(看到所有的问题和不一致在这里),我真的需要一个简单的方法来检查用户是否使用任何IE浏览器(包括11),但不包括Edge,因为Edge实际上非常好。

基于这里给出的答案,我写了一个简单的函数,返回一个全局布尔变量,然后您可以使用它。很容易检查IE。

var isIE;
(function() {
    var ua = window.navigator.userAgent,
        msie = ua.indexOf('MSIE '),
        trident = ua.indexOf('Trident/');

    isIE = (msie > -1 || trident > -1) ? true : false;
})();

if (isIE) {
    alert("I am an Internet Explorer!");
}

这样,您只需查找一次,并将结果存储在一个变量中,而不必在每次函数调用时获取结果。(据我所知,你甚至不需要等待文档准备好执行这段代码,因为用户代理与DOM无关。)

试着这样做

if ($.browser.msie && $.browser.version == 8) {
    //my stuff

}