You can use the property navigator
, accessible through the global object window
.
This property contains various information related to the user’s browser. For this answer, we will focus on the property userAgent
, containing a string
a little complicated and full of information. Among them, we can find the name of the browser in use. Something like this:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) Applewebkit/536 (KHTML, like Gecko) Chrome/72.0.3626.111 Safari/536.47
With the example below, you can detect which browser the user is using:
if (navigator.userAgent.includes('Chrome')) {
alert('Você usa o Google Chrome')
} else if (navigator.userAgent.includes('Opera')) {
alert('Você usa o Opera')
} else if (navigator.userAgent.includes('Firefox')) {
alert('Você usa o Mozilla Firefox')
} else if (navigator.userAgent.includes('Safari')) {
alert('Você usa o Safari')
} else if (navigator.userAgent.includes('MSIE')) {
alert('Você ainda usa IE? Como consegue?!')
} else {
alert('Você usa um navegador pouco comum...')
}