Last updated: Apr 4, 2024
Reading timeยท5 min
String.includes()
in JavaScriptTo get the browser name (e.g. Chrome, Firefox, Safar) in JavaScript:
RegExp.test()
method to check if a regular expression matches the
user agent string for each browser.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="browser-type"></h2> <br /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
function getBrowserType() { const test = regexp => { return regexp.test(navigator.userAgent); }; console.log(navigator.userAgent); if (test(/opr\//i) || !!window.opr) { return 'Opera'; } else if (test(/edg/i)) { return 'Microsoft Edge'; } else if (test(/chrome|chromium|crios/i)) { return 'Google Chrome'; } else if (test(/firefox|fxios/i)) { return 'Mozilla Firefox'; } else if (test(/safari/i)) { return 'Apple Safari'; } else if (test(/trident/i)) { return 'Microsoft Internet Explorer'; } else if (test(/ucbrowser/i)) { return 'UC Browser'; } else if (test(/samsungbrowser/i)) { return 'Samsung Browser'; } else { return 'Unknown browser'; } } const browserType = getBrowserType(); console.log(browserType); const browserTypeElement = document.getElementById('browser-type'); browserTypeElement.innerHTML = browserType;
You can start a basic development server by opening your terminal in the same
directory as the index.html
and index.js
files and issuing the following
command.
npx serve .
If I load the example in Chrome, I get the following output.
And here is a screenshot of opening the page in Firefox.
The navigator.userAgent property returns the user agent string for the current browser.
console.log(navigator.userAgent); // ๐๏ธ Chrome // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 // ๐๏ธ Firefox // Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0
The name of the browser is located toward the end of the user agent string (e.g. Chrome or Firefox).
We used the RegExp.test() method to check if the user agent string contains specific substrings.
The forward slashes mark the start and the end of the regular expressions / /
.
if (test(/chrome|chromium|crios/i)) { return 'Google Chrome'; }
The pipe |
symbol means "OR", e.g. chrome
or chromium
or crios
.
The i
flag stands for ignore
and does a case-insensitive search in string.
If the regex is matched in the string, we return the name of the corresponding browser.
String.includes()
in JavaScriptYou can also use the String.includes() method to get the browser name in JavaScript.
Here is an example that uses the method and some internal browser-specific properties.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="browser-type"></h2> <br /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
function getBrowserType() { if (isOpera()) { return 'Opera'; } else if (isEdge() || isEdgeChromium()) { return 'Microsoft Edge'; } else if (isChrome()) { return 'Google Chrome'; } else if (isFirefox()) { return 'Mozilla Firefox'; } else if (isSafari()) { return 'Apple Safari'; } else if (isInternetExplorer()) { return 'Microsoft Internet Explorer'; } else if (isUCBrowser()) { return 'UC Browser'; } else if (isSamsungBrowser()) { return 'Samsung browser'; } else { return 'Unknown browser'; } } const browserType = getBrowserType(); console.log(browserType); const browserTypeElement = document.getElementById('browser-type'); browserTypeElement.innerHTML = browserType; function isOpera() { return ( !!window.opr || !!window.opera || navigator.userAgent.toLowerCase().includes('opr/') ); } function isFirefox() { return ( navigator.userAgent.toLowerCase().includes('firefox') || typeof InstallTrigger !== 'undefined' ); } function isSafari() { return navigator.userAgent.toLowerCase().includes('safari'); } function isInternetExplorer() { return false || !!document.documentMode; } function isEdge() { return !isInternetExplorer() && !!window.StyleMedia; } function isChrome() { const userAgent = navigator.userAgent.toLowerCase(); return ( userAgent.includes('chrome') || userAgent.includes('chromium') || userAgent.includes('crios') ); } function isEdgeChromium() { return isChrome() && navigator.userAgent.includes('Edg'); } function isUCBrowser() { return navigator.userAgent.toLowerCase().includes('ucbrowser'); } function isSamsungBrowser() { return navigator.userAgent .toLowerCase() .includes('samsungbrowser'); }
Here is a screenshot of using the function in Chrome.
And here is a screenshot of using the function in Firefox.
And, here is an example of using the function in Opera.
We created reusable functions that check for each browser name.
The functions use the navigator.userAgent
property and some internal,
browser-specific properties.
function isChrome() { const userAgent = navigator.userAgent.toLowerCase(); return ( userAgent.includes('chrome') || userAgent.includes('chromium') || userAgent.includes('crios') ); }
Each function returns true
if the browser is of the expected type and false
otherwise.
We used the String.toLowerCase() method to convert the user agent string to lowercase before using String.includes() to check if it contains specific substrings in a case-insensitive manner.
You can also use the popular Bowser NPM package to get the browser name and version.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { margin: 100px; } </style> <script src=" https://cdn.jsdelivr.net/npm/bowser@2.11.0/es5.min.js "></script> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="browser-type"></h2> <br /> <script src="index.js"></script> </body> </html>
Notice that we load the bowser
package using a CDN.
Here is the related JavaScript file.
console.log(window.navigator.userAgent); const browser = bowser.getParser(window.navigator.userAgent); const browserObject = browser.getBrowser(); // { // "name": "Chrome", // "version": "113.0.0.0" // } console.log(browserObject); const browserName = browserObject.name; console.log(browserName); // ๐๏ธ 113.0.0.0 const browserVersion = browserObject.version; console.log(browserVersion); // ๐๏ธ Chrome
The browser.getBrowser()
method returns an object that contains the name
and
version
properties.
The name
property stores the name of the current browser and the version
property stores the version of the browser.
Here is a screenshot of the results in Google Chrome.
And here is a screenshot of the output in Firefox.
There is also a browser.getBrowserName()
method.
const browser = bowser.getParser(window.navigator.userAgent); console.log(browser.getBrowserName()); // ๐๏ธ Chrome
The example above uses a CDN script to load the Bowser library, however, you can also use the ES modules import syntax.
Here is the updated HTML file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { margin: 100px; } </style> </head> <body> <h2>bobbyhadz.com</h2> <h2 id="browser-type"></h2> <br /> <script type="module" src="index.js"></script> </body> </html>
Notice that we
set the type
attribute of the script
tag to module
.
Here is the related JavaScript code.
import bowser from 'https://cdn.jsdelivr.net/npm/bowser@2.11.0/+esm'; console.log(window.navigator.userAgent); const browser = bowser.getParser(window.navigator.userAgent); const browserObject = browser.getBrowser(); // { // "name": "Chrome", // "version": "113.0.0.0" // } console.log(browserObject); const browserName = browserObject.name; console.log(browserName); // ๐๏ธ 113.0.0.0 const browserVersion = browserObject.version; console.log(browserVersion); // ๐๏ธ Chrome
The example uses an ES modules import statement to import the bowser
module.
The remainder of the code is the same.
The bowser
package also provides you with additional information, e.g. the
operating system, the platform and the engine.
import bowser from 'https://cdn.jsdelivr.net/npm/bowser@2.11.0/+esm'; // { // "browser": { // "name": "Chrome", // "version": "113.0.0.0" // }, // "os": { // "name": "Linux" // }, // "platform": { // "type": "desktop" // }, // "engine": { // "name": "Blink" // } // } console.log(bowser.parse(window.navigator.userAgent));
The bowser.parse()
method takes the user agent string and returns an object
that contains information about the browser, the operating system, the platform
and the engine.
You can learn more about the related topics by checking out the following tutorials: