0

I am trying to detect internet explorer using ua.match, but this code is not working. Does anyone know why? (it works for other browers)

ua.match(/.*;MSIE (\.?\d+);.*/g)

Below is the code that works for Safari

ua.match(/.*Safari\/(\.?\d+).*/g)
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Soo Rhee
  • 19
  • 4
  • 1
    Could you explain *why* you want to do this? It's much easier and considered better practice to use feature detection (can this browser do ....?) See here... https://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection – Reinstate Monica Cellio Jan 05 '18 at 08:23
  • What is your error ? – Mohamed Abbas Jan 05 '18 at 08:23
  • @Archer the program simply returns the browser type of users, like this `if (ua.match(/.*;MSIE (\.?\d+);.*/g) != null) return PLATFORM_TYPE.msie;` – Soo Rhee Jan 05 '18 at 08:26
  • @MohamedAbbas I don't have problem with the compiling, but it returns 'undefined' brower type(which means the query should be wrong) – Soo Rhee Jan 05 '18 at 08:28
  • Yes, I understand fully what it is for. I'm asking *why* you want to do this as there may well be a much better way (for both you and the user). – Reinstate Monica Cellio Jan 05 '18 at 08:29

2 Answers2

2

IE 11 doesn't provide MSIE, it provides Trident. You should consider it;

var ua = navigator.userAgent;
if(ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident") > -1)
{
    console.log("IE");
}
lucky
  • 12,734
  • 4
  • 24
  • 46
0

from explorer ver 7 to 11, this one covers

if ( (navigator.appName == 'Netscape' && navigator.userAgent.search('Trident') != -1) || (ua.indexOf("msie") != -1) || (ua.indexOf('msie ')))
Soo Rhee
  • 19
  • 4