我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
当前回答
我把这段代码放在文档准备函数中,它只在internet explorer中触发。在Internet Explorer 11中测试。
var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {
//Do internet explorer exclusive behaviour here
}
其他回答
我只是想检查一下浏览器是否是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);}
这里有很多答案,我想补充我的意见。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无关。)
如果你使用的是jquery版本>=1.9,试试这个,
var browser;
jQuery.uaMatch = function (ua) {
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
/(Trident)[\/]([\w.]+)/.exec(ua) || [];
return {
browser: match[1] || "",
version: match[2] || "0"
};
};
// Don't clobber any existing jQuery.browser in case it's different
if (!jQuery.browser) {
matched = jQuery.uaMatch(navigator.userAgent);
browser = {};
if (matched.browser) {
browser[matched.browser] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if (browser.chrome) {
browser.webkit = true;
} else if (browser.webkit) {
browser.safari = true;
}
jQuery.browser = browser;
}
如果使用jQuery版本<1.9($。浏览器在jQuery 1.9中被移除)使用以下代码代替:
$('.myClass').on('click', function (event) {
if ($.browser.msie) {
alert($.browser.version);
}
});
对于任何版本的Internet Explorer都返回true:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
userAgent参数是可选的,默认为浏览器的用户代理。
这是另一种不用查看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。