检查用户是否正在使用IE [英] Check if user is using IE

查看:106
本文介绍了检查用户是否正在使用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;
}

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

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