我会通过点击具有特定类的div来调用以下类似的函数。
是否有一种方法可以在启动函数时检查用户是否正在使用Internet Explorer,并在他们使用其他浏览器时中止/取消它,以便它仅适用于IE用户?这里的用户都将在IE8或更高版本上运行,因此我不需要覆盖IE7和更低版本。
如果我能知道他们正在使用哪个浏览器,那就太好了,但并非必需。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
我会通过点击具有特定类的div来调用以下类似的函数。
是否有一种方法可以在启动函数时检查用户是否正在使用Internet Explorer,并在他们使用其他浏览器时中止/取消它,以便它仅适用于IE用户?这里的用户都将在IE8或更高版本上运行,因此我不需要覆盖IE7和更低版本。
如果我能知道他们正在使用哪个浏览器,那就太好了,但并非必需。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
function detectIE() {
var ua = window.navigator.userAgent;
var ie = ua.search(/(MSIE|Trident|Edge)/);
return ie > -1;
}
使用Modernizr
Modernizr.addTest('ie', function () {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ') > 0;
var ie11 = ua.indexOf('Trident/') > 0;
var ie12 = ua.indexOf('Edge/') > 0;
return msie || ie11 || ie12;
});
或者这个更简短的版本,如果浏览器是Internet Explorer,则返回true:
function isIe() {
return window.navigator.userAgent.indexOf("MSIE ") > 0
|| !!navigator.userAgent.match(/Trident.*rv\:11\./);
}
这是另一个简单易懂的函数,用于检测浏览器是否为IE(不包括并不差的Edge):
function isIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE '); // IE 10 or older
var trident = ua.indexOf('Trident/'); //IE 11
return (msie > 0 || trident > 0);
}
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($.browser在jQuery 1.9中已被删除),请改用下面的代码:
$('.myClass').on('click', function (event) {
if ($.browser.msie) {
alert($.browser.version);
}
});
var isIE = /*@cc_on!@*/false;
if(isIE){
//The browser is IE.
}else{
//The browser is NOT IE.
}
我知道这是一个老问题,但是如果有人再次遇到检测IE11的问题,这里有一个适用于所有当前版本的IE的有效解决方案。
var isIE = false;
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
isIE = true;
}
我用过这个。
function notIE(){
var ua = window.navigator.userAgent;
if (ua.indexOf('Edge/') > 0 ||
ua.indexOf('Trident/') > 0 ||
ua.indexOf('MSIE ') > 0){
return false;
}else{
return true;
}
}
死灵术。
为了不依赖于用户代理字符串,只需检查一些属性:
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(Anaheim):
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;
}
这个可以检测到Chromium:
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!");
}
基于这里提供的答案,我编写了一个简单的函数,返回一个全局布尔变量,你可以在后续使用它。很容易检查是否为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无关。)
随着现代Web开发的发展,支持非基于Chromium的浏览器是一种不良做法(其中Safari被认为根本不是基于Chromium的)
。很抱歉,但这种疯狂必须在某个时候以某种方式结束...... - user2173353