Enhancing the capabilities of a browser detection function in Javascript

Looking to enhance my Javascript browser detection function with input from others.

Concerns:

The assumption that "Chrome 18" is equivalent to "Maxthon 3" may not be accurate! How can we distinguish between Chrome 18 and Maxthon 3?

Similarly, how can we differentiate between Firefox and Sea Monkey?

Can we identify the Lunar browser using navigator.userAgent?

Are there any other popular browsers I should consider?

Please provide pure Javascript solutions/suggestions only!

 <!DOCTYPE html>

 <script type="text/javascript">

 function GetBrowser(){
 var browser="";
 var version=0;

 if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
  version=new Number(RegExp.$1);
  browser="FireFox";} else {

  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
   version=new Number(RegExp.$1);
   browser="Internet Explorer";} else {

   if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
    version=new Number(RegExp.$1);
    browser="Opera";} else {

    if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
     version=new Number(RegExp.$1);
     if (version==18) {version=3; browser="Maxthon";} else {browser="Google Chrome"}} else {version=0; browser="Undetermined";}
 }}}
 return browser+' '+version;}

 document.write(GetBrowser());
 </script>

Answer №1

Modernizr has a nifty browser/feature detection system that can be found at . It is also compatible with CDNs such as CDNjs which can be accessed via

I am interested in exploring how you can utilize this tool to detect supported browser features and easily identify different browsers based on these distinctions.

Answer №2

This is the final result I came up with. I fixed bugs in Opera and Safari version detections and added support for SeaMonkey.

 <!DOCTYPE html>

 <script type="text/javascript">

 function GetBrowser(){
 var browser="";
 var version=0;

 if (/SeaMonkey[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
  version=new Number(RegExp.$1);
  browser="SeaMonkey";} else {

  if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
   version=new Number(RegExp.$1);
   browser="Mozilla FireFox";} else {

   if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
    version=new Number(RegExp.$1);
    browser="Internet Explorer";} else {

    if (/Opera/.test(navigator.userAgent)){
     if (/Version[\/\s](\d+\.\d+)/.test(navigator.userAgent)){version=new Number(RegExp.$1);}
     browser="Opera";} else {

     if (/Maxthon[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
      version=new Number(RegExp.$1);
      browser="Maxthon";} else {

      if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
       version=new Number(RegExp.$1);
       browser="Google Chrome";} else {

       if (/Safari/.test(navigator.userAgent)){
        if (/Version[\/\s](\d+\.\d+)/.test(navigator.userAgent)){version=new Number(RegExp.$1);}
        browser="Safari";} else {browser="Unknown";}

 }}}}}}
 return browser+' '+version;}


 document.write(GetBrowser());

 </script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A server-side rendered page in Next.js that functions without the need

Hey everyone, I'm curious about the best way to serve an HTML page in a Next Js application without relying on additional JavaScript. I need this because I want to make sure my webpage can be accessed by users who have older phones like Symbian or oth ...

Exploring JSON array handling with jquery

Here is the JSON data I am working with: { "category": { "category_identification": "1", "category_name": "C1", "image_one": "1.PNG", "image_two": "1_.PNG", "logo": "LOGO_1.PNG", "category_description": "DESCRIPTION" }, "responseCo ...

Add an asterisk before each line of comment when working in a TypeScript file using the VS Code IDE

Within my VS Code workspace, I am using the Typescript language and would like to format my comments across multiple lines with a specific style (look out for the star character) /** *@desc any text * any text */ However, when I attempt to write a comm ...

Exploring arrays and objects in handlebars: A closer look at iteration

Database Schema Setup var ItemSchema = mongoose.Schema({ username: { type: String, index: true }, path: { type: String }, originalname: { type: String } }); var Item = module.exports = mongoose.model('Item',ItemSchema, 'itemi ...

Obtaining your CSGO inventory via Steam API using jsonp: A step-by-step guide

Hey there! I'm currently facing an issue while trying to access a player's CSGO inventory using Valve's API. Unfortunately, I keep running into the error message stating that no 'access-control-allow-origin' header is present on th ...

An issue has arisen in the production environment on AWS EC2 due to a problem with Nodemailer

When using nodemailer with node.js to send emails, I have set up the following configuration: var transporter = nodemailer.createTransport({ service: 'gmail', host: 'smtp.gmail.com', auth: { ...

The function WebForm_DoCallback is not recognized

Encountering an error where WebForm_DoCallback is undefined. UPDATE WebForm_DoCallback("AccountPageControl1", "FileSave~" + fileName, CVFileSavedServerResponse, null, null, true); function CVFileSavedServerResponse(param, context) { } Why isn't ...

Achieve the effect of making the Bootstrap JS Collapse text bold after it has been

Is there a way to make Bootstrap JS Collapse text Bold after it has been clicked on? <tr data-toggle="collapse" data-target="#demo8" class="accordion-toggle"> <td> <div class="fa ...

Is there a way to incorporate CSS into an element utilizing jQuery when only a class is available for identification, and when the time in the innerHTML is within a 5-minute range from the current time?

I have some HTML code that I need help with: <td class="mw-enhanced-rc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18:10&nbsp;</td> My goal is to use JavaScript to make the time bold. $('td[class^="mw-enhanced-rc"]').eac ...

The usage of an import statement is not permissible outside of a module

Having some trouble using the mathjs library to calculate complex solutions for the quadratic equation. No matter how I try to import the library into my code, I keep encountering errors. Initially, I attempted this method: At the top of my root.js file, ...

retrieving the values listed on the current v-data-table page

In my vuejs2 project, I am utilizing a v-data-table to display information in columns about a large number of users. Each page shows 25 users, with a total exceeding 60,000 individuals. I am wondering if there is a way to retrieve the list of users curre ...

Tips for changing between two texts within a button when clicked

Seeking a solution for toggling between two different texts inside a button when making an ajax call, with only one text displayed at a time. I'm facing difficulty in targeting the spans within the button specifically. Using 'this' as conte ...

Jquery fails to function properly unless the page is refreshed

On my MVC page, I have implemented a feature where certain text-boxes are shown or hidden based on the value selected in a drop-down menu using jQuery. The functionality works fine when the page is isolated, but when placed under a menu, it encounters a pr ...

The attribute 'NameNews' is not recognized in the specified type when running ng build --prod

Definition export interface INewsModule{ IDNews:number; IDCategoery:number; NameNews:string; TopicNews:string; DateNews?:Date; ImageCaption:string; ImageName:string ; } Implementation import { Component, OnInit, Input, I ...

WebWorker - Error in fetching data from server using Ajax call

I've been experimenting with making AJAX calls to an ajax.htm file using web workers. The goal is to have the data continuously updated at set intervals. Although I'm not seeing any errors and the GET request appears to be successful, the data i ...

Investigating unsuccessful requests in node.js

Here is my code: var request = require('request'); function Service(){ this._config = require('../path/to/config.json'); } Service.prototype.doThing = function(){ return new Promise(function(resolve, reject){ request.post(url, ...

What methods can be used to get npx to execute a JavaScript cli script on a Windows operating system

The Issue - While my npx scaffolding script runs smoothly on Linux, it encounters difficulties on Windows. I've noticed that many packages run flawlessly on Windows, but the difference in their operations remains elusive to me. Even after consulting A ...

Utilize JQuery variables for toggling the visibility of various DIV elements

On my webpage's splash page, there are 4 divs but only the home div is initially visible. The other three are hidden. Each of these divs has a button associated with it that triggers a jquery click event to swap out the currently visible div for the ...

Guide on how to navigate to the bottom of a div element using Selenium Webdriver

My current project involves a unique challenge where there is a specific div element on the webpage that acts as a popup dialog when a link is clicked (similar to Facebook reaction dialog boxes). To automate tests for this scenario, I am using Selenium We ...

Creating a default option in a Select tag with React when iterating over elements using the map method

After learning that each element in the dropdown must be given by the Option tag when using Select, I created an array of values for the dropdown: a = ['hai','hello','what'] To optimize my code, I wrote it in the following ...