Detect Internet Explorer in JavaScript
- 1 minute read
This JavaScript function returns true
if the user is using Internet Explorer, and it returns false
otherwise:
const usingInternetExplorer = () => {
return (navigator.userAgent.indexOf(`MSIE `) > - 1 || navigator.userAgent.indexOf(`Trident/`) > - 1);
};
In the below example, the usingInternetExplorer()
function is called inside a conditional statement:
if (!!usingInternetExplorer()) {
// The user is using Internet Explorer!
} else {
// The user is NOT using Internet Explorer!
}
Older versions of Internet Explorer include "MSIE "
in the
user agent string, while newer versions contain "Trident/"
.
But please remember that these user agent strings can be easily faked!
By the way, here’s a quick one-liner alternative with the exact same behavior, if you need it:
const usingInternetExplorer = () => (navigator.userAgent.indexOf(`MSIE `) > - 1 || navigator.userAgent.indexOf(`Trident/`) > - 1);
Conclusion
I’m sure we’ll never know why Internet Explorer changed its user agent string to be less easily recognizable…
Anyway, I hope this helped you out!