I\'m wondering how I could detect if the user viewing my website is using Internet Explorer 11 or below versions with Javascript.
It should be compatible and
Check for documentmode - the IE only property:
if (document.documentMode)
alert('this is IE');
Here you go, this should work for you:
//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");
}
}
I'd like to streamline the correct answer. This returns true
or false
:
function is_IE() {
return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}
Simpler version, returns boolean
function isIE() {
return !!window.navigator.userAgent.match(/MSIE|Trident/);
}