Sometimes, we want to detect Chrome and Safari browser with JavaScript.
In this article, we’ll look at how to detect Chrome and Safari browser with JavaScript.
How to detect Chrome and Safari browser with JavaScript?
To detect Chrome and Safari browser with JavaScript, we use the user agent and vendor strings.
For instance, we write
const isChrome =
/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
const isSafari =
/Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
if (isChrome) {
alert("You are using Chrome!");
}
if (isSafari) {
alert("You are using Safari!");
}
to check for Chrome with /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)
.
And we check for Safari with /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor)
.
userAgent
is the user agent string.
And vendor
is the vendor string.
Conclusion
To detect Chrome and Safari browser with JavaScript, we use the user agent and vendor strings.