我想知道如何使用Javascript检测用户是否使用Internet Explorer 11或以下版本。
它应该与所有这些版本兼容并且可以正常工作。
如何实现这一点?
我想知道如何使用Javascript检测用户是否使用Internet Explorer 11或以下版本。
它应该与所有这些版本兼容并且可以正常工作。
如何实现这一点?
这是给你的,应该可以为你工作:
//Per Icycool, one liner
//function isIE(){
// return window.navigator.userAgent.match(/(MSIE|Trident)/);
// }
function isIE() {
const ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
const msie = ua.indexOf('MSIE '); // IE 10 or older
const 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");
}
}
window.navigator.userAgent.match(/(MSIE|Trident)/)
。这段代码用于检测浏览器是否是 Internet Explorer 或 Microsoft Edge(使用 Trident 引擎)。 - Icycoolvar
而不是const
! - Roland Pihlakas检查 documentmode - 仅适用于IE的属性:
if (document.documentMode)
alert('this is IE');
我想要简化正确答案。这将返回true
或false
:
function is_IE() {
return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}
更简单的版本,返回布尔值
function isIE() {
return !!window.navigator.userAgent.match(/MSIE|Trident/);
}
/MSIE|Trident/.test(window.navigator.userAgent)
返回一个布尔值。 - Steve Clay
string
Trident
在window.navigator.userAgent
中。 - deEr.Trident
从IE 6
开始就存在了。两者都是。 - deEr.