检查用户是否正在使用带有jQuery的IE [英] Check if user is using IE with jQuery

查看:151
本文介绍了检查用户是否正在使用带有jQuery的IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过点击某个类的div来调用下面的函数。

I am calling a function like the one below by click on divs with a certain class.

如果用户正在使用Internet Explorer,我可以检查启动该功能的方法吗?如果他们使用其他浏览器就可以中止/取消它以便它只运行IE用户?这里的用户都将使用IE8或更高版本,所以我不需要覆盖IE7及更低版本。

Is there a way I can check when starting the function if a user is using Internet Explorer and abort / cancel it if they are using other browsers so that it only runs for IE users ? The users here would all be on IE8 or higher versions so I would not need to cover IE7 and lower versions.

如果我能告诉他们使用哪个浏览器那将是很棒,但不是必需的。

If I could tell which browser they are using that would be great but is not required.

示例功能:

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


推荐答案

使用以下JavaScript方法:

Use below JavaScript method :

function msieversion() 
{
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0) // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;
}

您可以在Microsoft支持网站下找到详细信息:

You may find the details on below Microsoft support site :

如何从脚本中确定浏览器版本

更新:(IE 11支持)

function msieversion() {

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;
}

这篇关于检查用户是否正在使用带有jQuery的IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆