Detect Internet Explorer using JavaScript

Quickly identify Internet Explorer with JavaScript for targeted browser-specific actions.

Usage

Using conditional comments

The following snippet works for Internet Explorer 6 through 11.

JavaScript
// Check if the browser is Internet Explorer
function isInternetExplorer() {
  // IE 6-11
  const isIE = /*@cc_on!@*/ false || !!document.documentMode;

  return isIE;
}

This code defines a function isInternetExplorer() that utilizes conditional comments (/*@cc_on!@*/) to determine if the browser is Internet Explorer. The function returns true if the browser is Internet Explorer and false otherwise. You can use this function to conditionally execute code based on the user’s browser.

ref: Conditional Comments in Internet Explorer

Using navigator object

To determine whether a user is using Internet Explorer, you can employ the following JavaScript code snippet:

JavaScript
function isInternetExplorer() {
    var ua = window.navigator.userAgent;
    
    // Check for Internet Explorer 6-10
    var msie = ua.indexOf("MSIE ");       
		
		// Check for Internet Explorer 11 and later, 
		// which uses the Trident engine
    var trident = ua.indexOf("Trident/");
    
    return msie > 0 || trident > 0;
}

checking the user agent string, may not be as accurate due to user agent spoofing or changes made by browser vendors.

Using ActiveXObject

Detecting the browser in use can be achieved by examining the navigator object. Internet Explorer provides a unique property, ActiveXObject, which can be utilized for identification.

Here’s a simple JavaScript function to detect Internet Explorer:

JavaScript
function isInternetExplorer() {
  // Check if ActiveXObject is defined
  return 'ActiveXObject' in window;
}

This function checks if the ActiveXObject is defined in the global window object, a characteristic unique to Internet Explorer.

IE version

JavaScript
function getIeVersion() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
	 var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
	 
	 if (ieversion>=8)
	 	return 8;
	  document.write("You're using IE8 or above")
	 else if (ieversion>=7)
	  document.write("You're using IE7.x")
	 else if (ieversion>=6)
	  document.write("You're using IE6.x")
	 else if (ieversion>=5)
	  document.write("You're using IE5.x")
	} else {
	 document.write("n/a")
	}
}

FAQs

Why detect Internet Explorer specifically?

Internet Explorer has been known for its non-standard behavior and lack of support for modern web standards. Web developers often need to detect it to apply workarounds or provide alternative solutions for a smoother user experience.

Is detecting browsers considered good practice?

While detecting browsers can be necessary for handling specific issues, it’s generally recommended to use feature detection rather than browser detection. Feature detection allows developers to check if a particular feature is supported, promoting a more forward-compatible and resilient codebase.

Are there any downsides to browser detection?

Browser detection can be prone to errors, especially as new browser versions are released. It might lead to unintentional consequences if a website doesn’t recognize a new browser or if the detection logic becomes outdated.

Can this code be used in conjunction with feature detection?

Yes, it’s common to use browser detection alongside feature detection. By combining both approaches, developers can create a more robust solution that adapts to the capabilities of the user’s browser.

References

  1. MDN Web Docs – Window.navigator.userAgent
  2. https://caniuse.com/
  3. Conditional Comments in Internet Explorer

Leave a Reply