How to detect the user’s browser with JavaScript

You can check which browser the user is running using plain JavaScript.

To detect the user browser, you need to analyze the property userAgent of the object navigator.

If you want to do something specific, for example, provide a polifill for a regula…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

You can check which browser the user is running using plain JavaScript.

To detect the user browser, you need to analyze the property userAgent of the object navigator.

If you want to do something specific, for example, provide a polifill for a regular expression when the browser is Safari, you do this:

if (navigator.userAgent.includes('Safari')) {
  // the user is running Safari
  // do something useful
}

On the other hand, if you want to do something for all browsers but Chrome, you check if the userAgent doesn’t include your search string:

if (!navigator.userAgent.includes('Chrome')) {
  // the user is NOT running Chrome
}

Using indexOf and toLowerCase

As an alternative to includes you can also use the indexOf method. If it returns -1, this means that the search string wasn’t found.

if (navigator.userAgent.indexOf('Chrome') < 0) {
  // the user is NOT running Chrome
}

If you’re not sure how exactly the user browser is spelled, you can try using the toLowerCase function on the navigator.userAgent.

if (navigator.userAgent.toLowerCase().indexOf('chrome') < 0) {
  // the user is NOT running Chrome
}

Learn Full Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-04-11T12:52:05+00:00) How to detect the user’s browser with JavaScript. Retrieved from https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/

MLA
" » How to detect the user’s browser with JavaScript." Coderslang: Become a Software Engineer | Sciencx - Sunday April 11, 2021, https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Sunday April 11, 2021 » How to detect the user’s browser with JavaScript., viewed ,<https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » How to detect the user’s browser with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/
CHICAGO
" » How to detect the user’s browser with JavaScript." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/
IEEE
" » How to detect the user’s browser with JavaScript." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/. [Accessed: ]
rf:citation
» How to detect the user’s browser with JavaScript | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/04/11/how-to-detect-the-users-browser-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.