use*_*510 340 javascript jquery internet-explorer browser-detection
我通过点击某个类的div来调用下面的函数.
有没有办法在用户使用Internet Explorer时检查何时启动该功能,如果他们使用其他浏览器就中止/取消它以便它只为IE用户运行?这里的用户都将使用IE8或更高版本,因此我不需要涵盖IE7及更低版本.
如果我可以告诉他们使用哪个浏览器那将是很好但不是必需的.
功能示例:
$('.myClass').on('click', function(event)
{
// my function
});
Run Code Online (Sandbox Code Playgroud)
Mar*_*rio 537
从Internet Explorer 12+(也称为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;
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
alert('IEEdge ' + detectIEEdge());
Run Code Online (Sandbox Code Playgroud)
IE 10的默认字符串:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Run Code Online (Sandbox Code Playgroud)
IE 11的默认字符串:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Run Code Online (Sandbox Code Playgroud)
IE 12的默认字符串(aka Edge):
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
Run Code Online (Sandbox Code Playgroud)
Edge 13的默认字符串(thx @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
Run Code Online (Sandbox Code Playgroud)
边缘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
Run Code Online (Sandbox Code Playgroud)
边缘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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
边缘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
Run Code Online (Sandbox Code Playgroud)
边缘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
Run Code Online (Sandbox Code Playgroud)
在CodePen进行测试:
http://codepen.io/gapcode/pen/vEJNZN
Spi*_*ode 436
使用以下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;
}
Run Code Online (Sandbox Code Playgroud)
您可以在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;
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*non 113
只是添加马里奥非常有用的答案.
如果您只想知道浏览器是否为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
}
Run Code Online (Sandbox Code Playgroud)
更新
我现在推荐这个.它仍然非常易读,代码少得多:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
Run Code Online (Sandbox Code Playgroud)
感谢JohnnyFun对缩短答案的评论:)
ben*_*ree 45
这将返回true任何版本的Internet Explorer:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
Run Code Online (Sandbox Code Playgroud)
该userAgent参数是可选的,默认为浏览器的用户代理.
joh*_*ith 27
您可以使用导航器对象来检测用户导航器,您不需要jquery
<script type="text/javascript">
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){
// do stuff with ie-users
}
</script>
Run Code Online (Sandbox Code Playgroud)
http://www.javascriptkit.com/javatutors/navigator.shtml
Thi*_*ark 24
这就是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;
Run Code Online (Sandbox Code Playgroud)
然后有几行代码分散在一起使用它作为一个数字,如
if (event === 'input' && msie <= 11) return false;
Run Code Online (Sandbox Code Playgroud)
和
if (enabled && msie < 8) {
Run Code Online (Sandbox Code Playgroud)
ken*_*238 19
你可以简单地这样做:
var isIE = window.document.documentMode ? true : false; // this variable will hold if the current browser is IE
Run Code Online (Sandbox Code Playgroud)
我知道这个问题很老了,但如果有人滚动到那么远,他们可以看到简单的答案:)
方法01:
$ .browser在jQuery 1.3版中已弃用,在1.9中已删除
if ( $.browser.msie) {
alert( "Hello! This is IE." );
}
Run Code Online (Sandbox Code Playgroud)
方法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]>
Run Code Online (Sandbox Code Playgroud)
方法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 );
}
Run Code Online (Sandbox Code Playgroud)
方法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);
}
}());
Run Code Online (Sandbox Code Playgroud)
function detectIE() {
var ua = window.navigator.userAgent;
var ie = ua.search(/(MSIE|Trident|Edge)/);
return ie > -1;
}
Run Code Online (Sandbox Code Playgroud)
或者这个非常简短的版本,如果浏览器是 Internet Explorer,则返回 true:
function isIe() {
return window.navigator.userAgent.indexOf("MSIE ") > 0
|| !!navigator.userAgent.match(/Trident.*rv\:11\./);
}
Run Code Online (Sandbox Code Playgroud)
我只想检查浏览器是否为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);}
Run Code Online (Sandbox Code Playgroud)
如果您使用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;
}
Run Code Online (Sandbox Code Playgroud)
如果使用jQuery 版本 <1.9($.browser 在 jQuery 1.9 中被删除),请改用以下代码:
$('.myClass').on('click', function (event) {
if ($.browser.msie) {
alert($.browser.version);
}
});
Run Code Online (Sandbox Code Playgroud)
Using 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;
});
Run Code Online (Sandbox Code Playgroud)
另一个简单(但人类可读)的函数来检测浏览器是否是 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
550865 次 |
| 最近记录: |