etecting user browser sometimes very useful if we want to write our logic based on the browser. Sometimes we
need to do some specific things
based on the browser or sometimes some functionality may not support in different browser so we need to
detect the user browser and write our logic.
To get the user browser, we have navigator object which contains the userAgent. And in this navigator.userAgent it contains the browser specific information.
So, let's see how we can detect the browser using navigator.userAgent.
Let's create a file with name detect-browser.html and write the below codes:
In the above code we have a button Detect browser and after clicking on this button, it'll call a function checkBrowser() And in this function we wrote the logic to detect the browser.
Test on Chrome:
Open the above file in the chrome browser and see the output as:
Now click on the Detect browser button and see the result. After clicking the button Detect browser you can see a popup with the browser name as:
Test on Firefox:
Now, open this html file into firefox browser and click on the button Detect browser and see the result as:
Test on Safari:
Now, open this html file into safari browser and click on the button Detect browser and see the result as: