我通过单击具有特定类的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
});
当前回答
更新SpiderCode的答案,以修复字符串'MSIE'返回-1但它匹配'Trident'的问题。它过去返回NAN,但现在在那个版本的IE中返回11。
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > -1) {
return ua.substring(msie + 5, ua.indexOf(".", msie));
} else if (navigator.userAgent.match(/Trident.*rv\:11\./)) {
return 11;
} else {
return false;
}
}
其他回答
这只适用于ie11版本以下。
var ie_version = parseInt(window.navigator.userAgent.substring(window.navigator.userAgent.indexOf("MSIE ") + 5, window.navigator.userAgent.indexOf(".", window.navigator.userAgent.indexOf("MSIE "))));
console.log(“版本号”,ie_version);
function msieversion() {
var ua = window.navigator.userAgent;
console.log(ua);
var msie = ua.indexOf("MSIE ");
if (msie > -1 || navigator.userAgent.match(/Trident.*rv:11\./)) {
// If Internet Explorer, return version numbe
// You can do what you want only in IE in here.
var version_number=parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)));
if (isNaN(version_number)) {
var rv_index=ua.indexOf("rv:");
version_number=parseInt(ua.substring(rv_index+3,ua.indexOf(".",rv_index)));
}
console.log(version_number);
} else {
//other browser
console.log('otherbrowser');
}
}
你应该在控制台看到结果,请使用chrome检查。
如果你只想知道浏览器是否是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
}
更新1:一个更好的方法
我现在就推荐这个。它仍然是非常可读的,并且代码更少:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
感谢JohnnyFun在评论中给出的简短答案:)
更新2:在CSS中测试IE
首先,如果可以的话,你应该使用@supports语句而不是JS来检查浏览器是否支持某个CSS特性。
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(注意IE根本不支持@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对于JS来说相对较新,我认为,在IE浏览器之外,它只能在IE11中工作。也可能是IE10。)
如果你在项目中使用SCSS (Sass),这可以简化为:
/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
更新3:添加Microsoft Edge(不推荐)
如果您还想将Microsoft Edge添加到列表中,您可以执行以下操作。但是我不推荐它,因为Edge是一个比IE强大得多的浏览器。
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}
对于任何版本的Internet Explorer都返回true:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
userAgent参数是可选的,默认为浏览器的用户代理。
更新SpiderCode的答案,以修复字符串'MSIE'返回-1但它匹配'Trident'的问题。它过去返回NAN,但现在在那个版本的IE中返回11。
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > -1) {
return ua.substring(msie + 5, ua.indexOf(".", msie));
} else if (navigator.userAgent.match(/Trident.*rv\:11\./)) {
return 11;
} else {
return false;
}
}