Detect Internet Explorer and display a warning message to change the browser

detect-internet-explorer-and-display-a-warning-message-to-change-the-browser

In this article, we will see how to detect Internet Explorer (IE) browser using JavaScript. The modern JavaScript technologies such as ReactJS, NodeJS, ExpressJS etc. requires modern browsers to work properly. As you all know that the Microsoft has ended the support for the Internet Explorer browser. The modern technologies also didn’t work fine on older or discountinued browsers.

Problem statement

You may see sites which says that the website is not supported with Internet Explorer e.g. one of the most popular website in the JavaScript developers is jsfiddle.net and when you open it in Internet Explorer you will see the following warning:

Caption: This image was taken in Feb 2020

You might want to create a website in the latest technology stack which works perfectly in all browsers and suddenly when you open it on Internet Explorer you may shout “What a holy shit! The site is not working at all”. The problem is with the Internet Explorer which Microsoft has stopped its support, even the Microsoft Security Chief says that the Internet Explorer is not a browser.

How to detect Internet Explorer?

There are different ways for detecting a browser and nowadays, you can also detect the platform of device and the operating system installed on it using both PHP and JavaScript.

In this article, we will detect Internet Explorer browser using simple JavaScript.

Please see the code below:

<script type='text/javascript'>
	function isItIE() {
	  user_agent = navigator.userAgent;
	  var is_it_ie = user_agent.indexOf("MSIE ") > -1 || user_agent.indexOf("Trident/") > -1;
	  return is_it_ie; 
	}
	if (isItIE()){
	    console.log('It is Internet Explorer');
	}else{
	    console.log('It is not Internet Explorer');
	}
</script>

Here you can see that we are using JavaScript’s native userAgent function so it is supported by all browsers.

From the above script you can not only detect Internet Exploerer but can also detect other browsers as well.

Just need to replace the value of indexOf() according to the required browser as below:

  • Chrome: indexOf("Chrome")
  • Firefox: indexOf("Firefox")
  • Safari: indexOf("Safari")
  • Edge: indexOf("Edge")
  • Opera: indexOf("Opera")
  • Internet Explorer: indexOf("Trident")

This is a standalone script and does not have any dependency on jQuery or any other JavaScript libraries.

Using this code you can detect the browser and can display an alert message to change the browser or whatever you want.

Please also see how to detect any browser using JavaScript.

Related Posts