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

Why isn't my textarea in jQUERY updating as I type?

On my website, I have a comment script that is not functioning correctly in some parts of the jQuery/JavaScript code. Instead of posting an edited comment to PHP, I created a notification window to test if the value passed through is actually changing. W ...

Is there a way to send a variable from a client-side script to a server-side script?

I am facing a challenge in Google App Maker where I am attempting to execute a Client Script to retrieve a variable and then pass that variable to a Server Script for further use. However, I am struggling to figure out the correct implementation. Within m ...

Exploring the power of DOM manipulation in both jQuery UI and AngularJS UI Router

I have a query regarding the integration of jQuery UI and AngularJS UI Router. My aim is to incorporate jQuery UI themes into my application, however, when using AngularJS UI Router, I face issues with an incomplete DOM tree. Is there a way to successfull ...

Utilize the Webstorm debugger in conjunction with node.js for seamless debugging

Several weeks ago, I attempted to get the webstorm debugger up and running, but unfortunately, it didn't work. Now, I'm giving it another shot, but I'm faced with the same outcome. I am following the instructions outlined here: http://www.j ...

After signing out and logging back in, data fails to display - ReactJS

I am encountering difficulties with the login/logout process and displaying data in a form. When I restart my server and log in without logging out, everything works fine and the data appears in the form. However, if I log in, then log out, and log back in ...

Is there a way to create a duplicate form without any pre-filled text when clicking the "next" button using JavaScript?

This form contains the details that I would like to display on the page again when clicking the "Add Another Module" button. <div> <form class="in-line" id="module_info"></form> <div style="display: flex;"> < ...

Discover Your Location in the Web Browser

My webpage includes the following JavaScript code to obtain the user's location: state.currentTimer = navigator.geolocation.watchPosition(success, error, { enableHighAccuracy: true, maximumAge: 5000 }); The page is designed to be accesse ...

Exploring jQuery Ajax: A Guide to Verifying Duplicate Names

When I apply the blur function to a textbox to check for duplicate names using jQuery AJAX, it works perfectly. Here is the code snippet: function checkForDuplicate(data){ $.post("test.php", {name: data}, function (data){ if(data){ ...

Why is it possible for me to call a function that is defined below a component?

My understanding was that in Javascript, functions could not be invoked if they are defined below where they're called (unless hoisting is involved). However, I discovered something interesting while working with React. The code snippet below actuall ...

In React version 16 and d3 version 4, if you try to use the mouse functionality from d3-selection, you may encounter a TypeError stating that it cannot read the property 'sourceEvent' of

Exploring the integration of d3 with React by incorporating the mouse feature from d3-selection module import { selectAll, select, mouse } from 'd3-selection'; Encountering an issue while attempting to utilize : mouse(e.target) or mouse(select( ...

Having trouble getting my list items to display on individual lines within the foreach loop. It just doesn't seem to be working as expected

In the event listener, I need to ensure that my list items within the forEach loop are not displaying on separate lines. This issue is causing a problem in a lengthy section of code. The goal is to update questions when an answer is clicked from a list. B ...

Utilizing Route Parameters in Node.js

frontend.jade doctype html html head meta(charset='utf-8') //if lt IE 9 script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js') // [if gte IE 9] <! scr ...

Enable automatic scrolling on a website using jQuery with the option to pause

I'm trying to implement a feature in jQuery where the web page auto-scrolls to a specific point, pauses for a moment, and then continues scrolling. It's like simulating a user scrolling down a page while reading an article - stopping and starting ...

Do I need to include a callback in my AWS Lambda handler function?

What is the function of the callback in the lambda handler? It appears to be utilizing the sns variable and I am looking to make some modifications to the variables. exports.handler = function(event, context, callback) { console.log("AWS lambda and ...

The parameter 'string | JwtPayload' cannot be assigned to the parameter 'string'

Utilizing Typescript alongside Express and JWT for Bearer Authorization presents a specific challenge. In this situation, I am developing the authorize middleware with JWT as specified and attempting to extricate the current user from the JWT token. Sampl ...

What are some effective techniques to optimize this node.js code and eliminate redundancy?

I am currently in the process of setting up a basic template for my demonstration project and writing my first node.js program. The piece of code below is functioning properly for my initial test, but it contains duplicated sections - Getting connection, E ...

Access the CSV file using Office365 Excel via a scripting tool

Objective I want to open a CSV file using Office365's Excel without actually saving the file on the client's machine. Challenge The issue with saving raw data on the client's machine is that it leads to clutter with old Excel files accumu ...

Display a list of errors from an array in JavaScript or jQuery, and output them into a designated <

I need assistance with displaying a list of error messages in a specific div. Within my code, I have a #error-list div and an array called errors that contains various error messages: var errors = ["First name is blank", "Last name is blank", "Company na ...

Problem with detecting collisions in Three.js

Currently, I am developing a simple game where players can add objects (cubes) to the scene at the position of a raycaster (mouse) click on a large ground plane. To prevent cubes from overlapping with each other, I have implemented basic collision detectio ...

What is the reason behind the code breaking when a newline is added between `return` and `(`?

After investigating my query, it seems that a peculiar issue arises in the code where setState fires and render method gets hit, but nothing rerenders The code functions correctly when there is no newline between the return statement and opening parenthes ...