-2

I have tried several ways to detect MS Explorer with JavaScript but failed. Used alert in JavaScript without using codes for detecting Explorer and the JavaScript works. Why can't any of my codes work on any browsers?

var isIE = /*@cc_on!@*/false;
window.onload = function() {
 
var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {

    window.alert("This Explorer");
 }
  
}

I tried several ways to detect MS Explorer using JavaScript.I used alert... in JavaScript and it worked. It works as long as I do not try to detect Explore. What am I doing wrong?

Niel-A
  • 1
  • 4
  • 3
    Possible duplicate of [How to detect Safari, Chrome, IE, Firefox and Opera browser?](https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser) – Luca Kiebel Aug 10 '18 at 17:26

1 Answers1

1

It takes a simple JS script to accomplish this for any borwser out there that supports JS.

For IE:

function isIE() {
  ua = navigator.userAgent;
  var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;
  return is_ie; 
}

if (isIE()) {
    alert('It is InternetExplorer');
} else {
    alert('It is NOT InternetExplorer');
}

Hope this helps.

StudioTime
  • 22,603
  • 38
  • 120
  • 207
JKimbrough
  • 226
  • 1
  • 8
  • It worked! Well, I will have to break your code down to figure out why yours works and not mine. Thank you very much. – Niel-A Aug 10 '18 at 19:49
  • You are very welcome. You never defined ms_ie in your code - that is probably where you went wrong. – JKimbrough Aug 10 '18 at 21:25
  • One other thing. Why can't I eliminate the else statement and still have it work. I have never seen anything like this. Makes no sense – Niel-A Aug 10 '18 at 22:10
  • 1
    I had not realized that I was in Edge not explorer. – Niel-A Aug 24 '18 at 23:05