Trying to make a script that detects if the browser is Chrome, if not do code, if so, do code

138 Views Asked by At

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?

1

There are 1 best solutions below

0
Xavin On

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.