9 javascript internet-explorer
我想知道如何检测我的网站用户是否使用Internet Explorer 11或更低版本的Javascript.
它应该兼容并适用于所有这些版本.
怎么实现呢?
Rya*_*son 20
在这里,这应该适合你:
//Per Icycool, one liner
//function isIE(){
// return window.navigator.userAgent.match(/(MSIE|Trident)/);
// }
function isIE() {
var ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
var msie = ua.indexOf('MSIE '); // IE 10 or older
var trident = ua.indexOf('Trident/'); //IE 11
return (msie > 0 || trident > 0);
}
//function to show alert if it's IE
function ShowIEAlert(){
if(isIE()){
alert("User is using IE");
}
}
Run Code Online (Sandbox Code Playgroud)
我想简化正确的答案。这将返回true或false:
function is_IE() {
return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}
Run Code Online (Sandbox Code Playgroud)
检查documentmode - IE 唯一的属性:
if (document.documentMode)
alert('this is IE');
Run Code Online (Sandbox Code Playgroud)