JavaScript – Detecting Firefox Browser

The code below shows you the way how to detect if a browser is Firefox or not using JavaScript.

To do this, we simply look at User-Agent data (navigator.userAgent), if it contains ‘firefox’, the browser is Firefox. Otherwise, the browser is something else, it can be Chrome, Edge, Safari, or whatever.

The code looks simple as this:

firefox-detection.js
function detectFirefox() {
    var userAgent = navigator.userAgent.toLowerCase();
    return (userAgent.indexOf('firefox') !== -1);
}

 

Use the function:

// mozilla/5.0 (windows nt 10.0; win64; x64; rv:60.0) gecko/20100101 firefox/60.0
if (detectFirefox()) {
    console.log('The browser is Firefox');
} else {
    console.log('The browser is not Firefox');
}

Output:

The browser is Firefox

Leave a Reply

avatar