0

I'm trying to make a Script that will detect if you have Chrome, and then do some code, if not, do other code. I'm using DetectJS to detect the browsers, this is my code right now:

const ua = detect.parse(navigator.userAgent);

function nameContinue() {
  if (ua.browser.family == "Chrome") {
    console.log("browser (" + ua.browser.family + ") is supported");
    //Do some code
  } else {
    console.log("browser (" + ua.browser.family + ") is not supported");
    //Do some other code
  }
}
<button onClick="nameContinue()">Click me to run code</button>

<script src="https://vendor.scribblenerd.com/detectjs-1.0.0-dist/js/detect.min.js"></script>
<script src="js/form.js"></script>

It works fine when I test it on Firefox, but in Safari, and Mobile Safari, the console prints out

browser (Safari) is supported

even though it should be saying that its not supported in console... any ideas?

  • Does this answer your question? [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) – IMSoP May 04 '20 at 13:14
  • I tried that JSFiddle in Safari just a second ago and every var is false, even the Safari one... https://imgur.com/a/tEONTGl – Scribble Nerd May 04 '20 at 13:19

1 Answers1

0

Instead of checking browser.family you could try with ua.browser.name and some technique to find a string contains a substring.

For instance:

if (ua.browser.name.toLowerCase().indexOf("chrome") >= 0)

Provably more than one browser use the same family or anyone not share their ones.

Xavin
  • 66
  • 4