0

How to make a check on a client’s browser on a website? I was given the task that the website was supported only by Google Chrome or Firefox

When user uses other browsers, a blank page should appear with text similar to

Your browser is not supported by the system.
Use Google Chrome or Firefox Browsers
domsson
  • 4,553
  • 2
  • 22
  • 40
  • 7
    Are we back at the early 2000s? "This website is best viewed at 800x600 resolution on Internet Explorer 6"? – VLAZ Jan 23 '20 at 11:51
  • 1
    I was given the task at the job... i just have to do it... – Yerassyl Kelsingazin Jan 23 '20 at 11:54
  • 1
    No Safari? No Opera? No Chromium? No Edge? No Konqueror? No Lynx? No surf? No ... --- while I understand that you just want to implement the given task, I would strongly suggest to advice your employer on how this is most likely a very bad idea. – domsson Jan 23 '20 at 11:58
  • 1
    @domsson it's absolutely a *terrible* idea. Feature detection should be used whenever possible. At my current job we do only support certain browsers (well, the biggest ones) but we don't hit the user with an obnoxious "can't use this" message. We've just documented the acceptable browsers and if somebody tries to use anything outside of them (extremely slim chance but still) it will just not work very well. But as long as the browser supports features common to the other big browsers, it'd work fine. – VLAZ Jan 23 '20 at 12:03
  • @VLAZ yeah, I am in the same situation. Our website also does not work well in other browsers, that it is why head want to restrict other browsers except chrome and firefox. I tried to suggest an advice to employer that it`s a bad move, but they support their decision... – Yerassyl Kelsingazin Jan 24 '20 at 11:22

1 Answers1

0

By checking the string navigator.userAgent

Check the function bellow, and adapt it to your needs.

function wichBrowser() {
  // CHROME
  if (navigator.userAgent.indexOf("Chrome") > -1 ) {
    return "Google Chrome"
  }
  // FIREFOX
  else if (navigator.userAgent.indexOf("Firefox") > -1 ) {
    return "Mozilla Firefox"
  }
  // INTERNET EXPLORER
  else if (navigator.userAgent.indexOf("MSIE") > -1 ) {
    return "Internet Exploder"
  }
  // EDGE
  else if (navigator.userAgent.indexOf("Edge") > -1 ) {
    return "Edge"
  }
  // SAFARI
  else if (navigator.userAgent.indexOf("Safari") > -1 ) {
    return "Safari"
  }
  // OPERA
  else if (navigator.userAgent.indexOf("Opera") > -1 ) {
    return "Opera"
  }
  // YANDEX BROWSER
  else if (navigator.userAgent.indexOf("Opera") > -1 ) {
    return "YaBrowser"
  }
  // OTHERS
  else {
    return "Others"
  }
};
  • you missed something in the last if condition! – rehman_00001 Jan 23 '20 at 12:12
  • So, Edge is going to be identified as Chrome or Safari? Because the useragent contains both of those strings. The only reason to not be identified as Firefox is because the useragent also says "Mozilla" – VLAZ Jan 23 '20 at 12:19