jQuery 检查用户是否使用 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19999388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if user is using IE
提问by user2571510
I am calling a function like the one below by click on divs with a certain class.
我通过单击具有特定类的 div 来调用如下所示的函数。
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.
如果用户正在使用 Internet Explorer,是否可以在启动该功能时进行检查,如果他们正在使用其他浏览器,我可以中止/取消它,以便它仅对 IE 用户运行?这里的用户都在 IE8 或更高版本上,所以我不需要涵盖 IE7 和更低版本。
If I could tell which browser they are using that would be great but is not required.
如果我能告诉他们使用的是哪个浏览器,那会很棒,但不是必需的。
Example function:
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
回答by Mario
It's several years later, and the Edge browser now uses Chromium as its rendering engine.
Checking for IE 11 is still a thing, sadly.
几年后,Edge 浏览器现在使用 Chromium 作为其渲染引擎。
遗憾的是,检查 IE 11 仍然是一件事。
Here is a more straightforward approach, as ancient versions of IE should be gone.
这是一个更直接的方法,因为旧版本的 IE 应该消失了。
if (window.document.documentMode) {
// Do IE stuff
}
Here is my old answer (2014):
这是我的旧答案(2014):
In Edge the User Agent Stringhas changed.
在 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;
}
Sample usage:
示例用法:
alert('IEEdge ' + detectIEEdge());
Default string of IE 10:
IE 10 的默认字符串:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Default string of IE 11:
IE 11 的默认字符串:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Default string of Edge 12:
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
Default string of Edge 13 (thx @DrCord):
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
Default string of Edge 14:
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
Default string of Edge 15:
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
Default string of Edge 16:
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
Default string of Edge 17:
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
Default string of Edge 18 (Insider preview):
Edge 18 的默认字符串(内幕预览):
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
Test at CodePen:
在 CodePen 上测试:
回答by SpiderCode
Use below JavaScript method :
使用以下 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;
}
You may find the details on below Microsoft support site :
您可以在以下 Microsoft 支持网站上找到详细信息:
How to determine browser version from script
Update :(IE 11 support)
更新:(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;
}
回答by Daniel Tonon
If all you want to know is if the browser is IE or not, you can do this:
如果您只想知道浏览器是否是 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
}
Update 1: A better method
更新 1:更好的方法
I recommend this now. It is still very readable and is far less code :)
我现在推荐这个。它仍然非常易读,而且代码少得多:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
Thanks to JohnnyFun in the comments for the shortened answer :)
感谢 JohnnyFun 在评论中缩短答案:)
Update 2: Testing for IE in CSS
更新 2:在 CSS 中测试 IE
Firstly, if you can, you should use @supports
statements instead of JS for checking if a browser supports a certain CSS feature.
首先,如果可以,您应该使用@supports
语句而不是 JS 来检查浏览器是否支持某个 CSS 功能。
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(Note that IE doesn't support @supports
at all and will ignore any styles placed inside an @supports
statement.)
(请注意,IE 根本不支持@supports
,并且会忽略放置在@supports
语句中的任何样式。)
If the issue can't be resolved with @supports
then you can do this:
如果问题无法解决,@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 */
}
(Note: classList
is relatively new to JS and I think, out of the IE browsers, it only works in IE11. Possibly also IE10.)
(注意:classList
JS 相对较新,我认为,在 IE 浏览器中,它仅适用于 IE11。可能也适用于 IE10。)
If you are using SCSS (Sass) in your project, this can be simplified to:
如果您在项目中使用 SCSS (Sass),这可以简化为:
/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
Update 3: Adding Microsoft Edge (not recommended)
更新 3:添加 Microsoft Edge(不推荐)
If you also want to add Microsoft Edge into the list, you can do the following. However I do not recommend itas Edge is a much more competent browser than IE.
如果您还想将 Microsoft Edge 添加到列表中,您可以执行以下操作。但是我不推荐它,因为 Edge 是一个比 IE 更胜任的浏览器。
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}
回答by bendytree
This returns true
for any version of Internet Explorer:
这true
对于任何版本的 Internet Explorer 都会返回:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
The userAgent
parameter is optional and it defaults to the browser's user agent.
该userAgent
参数是可选的,默认为浏览器的用户代理。
回答by john Smith
You can use the navigator object to detect user-navigator, you don't need jquery for it
您可以使用 navigator 对象来检测用户导航器,您不需要 jquery
<script type="text/javascript">
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){
// Do stuff with Internet-Exploders ... :)
}
</script>
回答by ThisClark
This is how the Angularjs team is doing it (v 1.6.5):
这是 Angularjs 团队的做法(v 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;
Then there are several lines of code scattered throughout using it as a number such as
然后有几行代码分散在整个使用它作为一个数字,例如
if (event === 'input' && msie <= 11) return false;
and
和
if (enabled && msie < 8) {
回答by gdibble
Using the answers above; simple & condensed returning Boolean:
使用上面的答案;简单和浓缩返回布尔值:
var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);
var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);
回答by Aamir Shahzad
Method 01:
$.browser was deprecated in jQuery version 1.3 and removed in 1.9
方法 01:
$.browser 在 jQuery 1.3 版中已弃用,并在 1.9 版中删除
if ( $.browser.msie) {
alert( "Hello! This is IE." );
}
Method 02:
Using Conditional Comments
方法 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]>
Method 03:
方法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. );
}
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 );
}
Method 04:
Use JavaScript/Manual Detection
方法 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);
}
}());
回答by Bill_VA
I just wanted to check if the browser was IE11 or older, because well, they're crap.
我只是想检查浏览器是否是 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);}
回答by headione
function detectIE() {
var ua = window.navigator.userAgent;
var ie = ua.search(/(MSIE|Trident|Edge)/);
return ie > -1;
}