-3

I have been trying to detect the user's browser. So I checked my code in Chrome and Edge. It doesn't work in both.

window.addEventListener("load", function() {
  let userAgentString = navigator.userAgent;
  console.log(userAgentString);
  let isChrome = userAgentString.indexOf("Chrome") > -1;

  if (isChrome) {
    $('#chromeUser').css('display', 'block');
  }
});

It makes the div chromeUser display to block in both browsers. It means that it doesn't know which browser I'm from.

Ideas anyone?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Helena B.
  • 1
  • 1
  • Your code appears to be correct, how is it not working? – easrng Apr 23 '20 at 13:39
  • @Perhapsyouseethisname. that what I don't understand. – Helena B. Apr 23 '20 at 13:41
  • What problem are you **actually** trying to solve by doing browser detection? If Edge is missing some feature that Chrome has, you should try to determine if that **feature** is available. Otherwise you are going to play whack-a-mole adjusting for each specific browser. – zero298 Apr 23 '20 at 13:48
  • @zero298 Sorry but I'm really don't know about this comparison between browsers with javascript code. where to start? – Helena B. Apr 23 '20 at 13:50
  • What we meant here by feature detection is that normally you'd for example want to show element X on browser Y because only that browser supports element X. In that case, you should instead detect _whether the browser (whichever it is) supports X_ and not whether it is exactly browser Y. (Also, tomorrow browser Z may also start supporting X...) – CherryDT Apr 23 '20 at 13:53
  • However, now that you mentioned it's about showing a Flash deprecation message, this is entirely irrelevant anyway, because _all browsers will drop Flash_. – CherryDT Apr 23 '20 at 13:53
  • Edge, Opera and Brave are the same engine as Chrome, IE support for Flash is separately dropped, Firefox [drops it too](https://developer.mozilla.org/en-US/docs/Plugins/Roadmap) and any browser still supporting it after Dec. 2020 is a bad old browser which you shouldn't support anyway, because it means it'll be an insecure browser. Because [Adobe will stop shipping security updates for Flash at the end of 2020](https://theblog.adobe.com/adobe-flash-update/)! – CherryDT Apr 23 '20 at 13:56

2 Answers2

0

Microsoft Edge is now a version of Chromium, and its user agent is "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36 Edg/81.0.416.62", so your code is identifying it as Chrome. Are there any specific issues you are having with other browsers? If so, you should try to detect those issues or features instead of detecting browsers, which is a bad practice.

easrng
  • 395
  • 4
  • 10
0

Try to test on another browser. Edge is based on Chromium (chrome)

The user-agent looks like : User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43

read more here: https://learn.microsoft.com/en-us/microsoft-edge/web-platform/user-agent-string

Segev -CJ- Shmueli
  • 1,535
  • 14
  • 15
  • I'm tell you what. Google Chrome will stop support flash files at the end of 2020. I want to check if user is from Google Chrome and show a message in accordance. did you think Edge will block flash too? – Helena B. Apr 23 '20 at 13:47
  • [Yes.](https://blogs.windows.com/msedgedev/2019/08/30/update-removing-flash-microsoft-edge-internet-explorer/#HQ8sCXmIatAsCcLU.97) – CherryDT Apr 23 '20 at 13:48
  • 1
    You should probably show whatever message this is for _all_ browsers because eventually every good, maintained browser will remove Flash soon. [Flash is dead](https://www.wired.com/story/adobe-finally-kills-flash-dead/) and Adobe won't support it anymore after the end of the year, so no updates or anything, meaning it would be irresponsible by any browser vendor to keep supporting it, it would be a security nightmare. – CherryDT Apr 23 '20 at 13:48
  • At this time the support for flash is on a client to client basis. meaning - you can have Chrome where it is disabled by default, or you may have a browser where the user enabled flash specifically from the browser's settings. There are scripts you can run to check if flash is enabled. https://stackoverflow.com/questions/998245/how-can-i-detect-if-flash-is-installed-and-if-not-display-a-hidden-div-that-inf – Segev -CJ- Shmueli Apr 23 '20 at 14:57