我会通过点击具有特定类的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
});
几年过去了,Edge浏览器现在使用Chromium作为其渲染引擎。
遗憾的是,仍然需要检查IE 11。
以下是更直接的方法,因为古老版本的IE应该已经消失了。
if (window.document.documentMode) {
// Do IE stuff
}
这是我的旧答案(2014):
在Edge浏览器中,用户代理字符串已经改变了。
/**
* detect IEEdge
* returns version of IE/Edge or false, if browser is not a Microsoft browser
*/
function detectIEEdge() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
示例用法:
alert('IEEdge ' + detectIEEdge());
IE 10 的默认字符串:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
IE 11的默认字符串:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Edge 12 的默认字符串:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0
Edge 13 的默认字符串(感谢 @DrCord):
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
Edge 14 的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/14.14300
Edge 15 的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Edge 16的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
Edge 17 的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
Edge 18(Insider preview)的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17730
CodePen测试:
使用以下 JavaScript 方法:
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支持网站上找到详细信息:
更新: (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;
}
if (msie > 0)
是具有误导性的。如果未找到该值,则indexOf()函数返回-1而不是0。因此,if (msie > -1)
更能解释清楚。 - Neville NazeraneIE 11
:https://dev59.com/MGIj5IYBdhLWcg3wrG8q#21712356 - Leniel Maccaferri/Edge\/|Trident\/|MSIE /.test(window.navigator.userAgent)
这段代码可以在 IE10 和 IE11 上运作。如果您能验证是否适用于 IE9 及以下版本和 Edge 浏览器,请编辑答案。 - Indolering如果您只想知道浏览器是否为IE,可以这样做:
var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');
if ((old_ie > -1) || (new_ie > -1)) {
isIE = true;
}
if ( isIE ) {
//IE specific code goes here
}
我现在推荐这个方法。它仍然很易读,而且代码量要少得多 :)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
首先,如果可以的话,应该使用@supports
语句而不是JS来检查浏览器是否支持某个CSS特性。
感谢评论区的JohnnyFun提供缩短版答案 :)
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
@supports
,并将忽略放置在@supports
语句中的任何样式。)@supports
无法解决问题,则可以执行以下操作:// JS
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
document.documentElement.classList.add('ie')
}
/* CSS */
.element {
/* styles that apply everywhere */
}
.ie .element {
/* styles that only apply in IE */
}
classList
在JavaScript中相对较新,我认为除了IE11外,在IE浏览器中不起作用。可能也适用于IE10。)/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
如果您也想将微软 Edge 添加到列表中,可以按照以下步骤进行操作。然而,我不建议这样做,因为 Edge 是一款比 IE 更强大的浏览器。
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}
ms_ie = !!ua.match(/MSIE|Trident/)
(该代码片段用于检查用户代理字符串中是否包含“MSIE”或“Trident”,并将结果存储在名为“ms_ie”的变量中,该变量的值为布尔类型。) - xoriimport isIE from './isIE.js'
。现在另一个组件出于某种原因需要检查IE,import isIE from './isIE.js'
。如果这还不足以说服您,那么它对于可读性来说更好。使用变量有两个原因:减少代码重复;澄清代码的含义。当您阅读 if (/MSIE|Trident/.test(window.navigator.userAgent))
时,与 if (isIE)
相比,它要不那么明显。 - Daniel Tonon此代码适用于任何版本的Internet Explorer,其返回值为true
:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
参数userAgent
是可选的,默认为浏览器的用户代理。
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){
// Do stuff with Internet-Exploders ... :)
}
以下是 AngularJS 团队的实现方式(版本 1.6.5):
var msie, // holds major version number for IE, or NaN if UA is not IE.
// Support: IE 9-11 only
/**
* documentMode is an IE-only property
* http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
*/
msie = window.document.documentMode;
然后散布在代码中有几行使用它作为数字的代码,例如
if (event === 'input' && msie <= 11) return false;
和
if (enabled && msie < 8) {
document.documentMode
支持 IE8+。对于 Edge 或 Chrome/FireFox,它将返回 'undefined'
... 我将此代码更改为 var IEver = window.document.documentMode || (window.attachEvent? 1 : 99);
,以便在 IE8+ 中返回确切的 IE 版本,对于非 IE 浏览器返回 99(通常是现代浏览器),而对于旧版 IE5-7 返回 1。这样编写是因为我们通常只需要为一些旧版 IE 进行特殊处理。所以,如果 (IEver < 9)
,则表示它是旧版 IE。 - S.Serpooshan你可以简单地这样做:
var isIE = window.document.documentMode ? true : false; // this variable will hold if the current browser is IE
我知道这个问题很老,但是如果有人滚动到那里,他们可以看到简单的答案 :)方法 01:
$.browser 已在 jQuery 1.3 版本中被弃用并在 1.9 版本中移除。
if ( $.browser.msie) {
alert( "Hello! This is IE." );
}
方法 02:
使用条件注释
<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->
<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->
<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>
方法03:
/**
* Returns the version of Internet Explorer or a -1
* (indicating the use of another browser).
*/
function getInternetExplorerVersion()
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
方法04:
使用JavaScript / 手动检测
/*
Internet Explorer sniffer code to add class to body tag for IE version.
Can be removed if your using something like Modernizr.
*/
var ie = (function ()
{
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i>< ![endif]-->',
all[0]);
//append class to body for use with browser support
if (v > 4)
{
$('body').addClass('ie' + v);
}
}());
.ie10
类是最好的选择之一。因为Internet Explorer 10会在HTML元素<html class="ie10">
中添加".ie10"
类,所以你可以像这样使用它:.ie10 .myclass {//some css here}
。 - Aamir Shahzad利用上述答案,简洁明了地返回布尔值:
var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);
我只是想检查一下浏览器是否为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);}
随着现代Web开发的发展,支持非基于Chromium的浏览器是一种不良做法(其中Safari被认为根本不是基于Chromium的)
。很抱歉,但这种疯狂必须在某个时候以某种方式结束...... - user2173353