Check for IE browser with JavaScript/jQuery
This post will discuss how to check if a user is using an IE browser with JavaScript.
Internet Explorer (IE) was once the most popular and widely used browser in the world, but it has been gradually losing its market share to other modern browsers, such as Chrome, Firefox, Safari, and Edge. IE is known for its poor performance, security issues, and lack of support for many web standards and features.
However, there are still some users who use IE for various reasons, such as legacy applications, corporate policies, or personal preferences. Therefore, as a web developer, we may encounter situations where we need to check if the user’s browser is IE or not, and perform some browser-specific code or actions accordingly. For example, we may want to display a warning message, redirect to another page, or enable or disable some features based on the browser detection.
1. Using document.documentMode property
The simplest method to check for IE browser with JavaScript is to use the window.document.documentMode property. This property is a unique feature of IE that returns the mode used by the browser to render the current document. The mode can be either a number that represents the compatibility mode version (such as 5, 7, 8, 9, 10, or 11), or undefined if the browser is not IE or is in standard mode. For example:
|
1 2 3 4 5 6 7 8 9 10 |
function isIE() { return document.documentMode; } if (isIE()) { alert("Internet Explorer"); } else { alert("Not Internet Explorer"); } |
The documentMode returns the undefined value on all other browsers. For the Internet Explorer browser, it returns the IE mode used to render the document. It works with any version of IE that supports the window.document.documentMode property, which is IE 8 and above.
2. Using the navigator.userAgent Property
Another way to check for IE browser with JavaScript is to use the NavigatorID.userAgent property. This property returns the user agent of the browser, which is a string that identifies the browser name, version, platform, and other information. The navigator object is a global object that provides various methods and properties for working with the browser. To determine for the IE browser, we should check for MSIE or Trident in the user agent string. We can use a regular expression (RegEx) to match the user agent string with a pattern that matches IE browser.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function isIE() { // Access the navigator.userAgent property var userAgent = navigator.userAgent; // Create a RegEx pattern that matches the user agent string of IE browser let pattern = /MSIE|Trident/i; // Use the `test()` method to check if the user agent string matches the pattern return pattern.test(userAgent); } if (isIE()) { alert("Internet Explorer"); } else { alert("Not Internet Explorer"); } |
Alternatively, we can use the String.prototype.includes() method or the String.prototype.indexOf() method to check if the user agent string contains “MSIE” or “Trident”, which are indicators of IE browser. Here is an example with the indexOf() method:
|
1 2 3 4 5 6 7 8 9 10 11 |
function isIE() { var userAgent = navigator.userAgent; return (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1); } if (isIE()) { alert("Internet Explorer"); } else { alert("Not Internet Explorer"); } |
This method works with any version of IE that has a user agent string that contains “MSIE” or “Trident”, which are IE 6 and above. However, this approach is unreliable since users can modify the browser user agent string. Instead, we should use the document.documentMode property, which is IE-specific.
3. Using jQuery
To check for IE browser with jQuery, we can use feature detection, which checks if the browser supports certain features or functionalities. We can use the jQuery.support property, which contains a collection of properties that indicate the presence of different browser features or bugs. For example, we can use the following code:
|
1 2 3 4 5 6 |
// Check if the browser supports CORS if (jQuery.support.cors) { console.log("Your browser supports CORS!"); } else { console.log("Your browser does not support CORS!"); } |
That’s all about checking for IE browser with JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)