Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Desktop Browser (not mobile) with Javascript

I found the following code to detect a desktop browser. But the method also detects some mobile browsers. How can I detect only desktop browsers like Safari, IE, Firefox, Opera etc?

is_desktopBrowser : function() {     var ua = navigator.userAgent.toLowerCase();      var rwebkit = /(webkit)[ \/]([\w.]+)/;     var ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/;     var rmsie = /(msie) ([\w.]+)/;     var rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/;      var match = rwebkit.exec(ua) ||             ropera.exec(ua) ||             rmsie.exec(ua) ||             ua.indexOf("compatible") < 0 && rmozilla.exec(ua) ||             [];      return { browser: match[1] || "", version: match[2] || "0" }; }, 
like image 627
fabian Avatar asked Nov 03 '11 13:11

fabian


People also ask

Can JavaScript detect browser type?

Browser Detection with JavaScript. If you really must do it, detecting what browser someone is using is easy with JavaScript. JavaScript has a standard object called navigator that contains data about the browser being used.

How can I tell if a device is mobile JavaScript?

To detect if the user is using a mobile device in JavaScript, we can use the userAgent property. This property is part of the navigator object and sent by the browser in HTTP headers. It contains information about the name, version, and platform of the browser.

How do I know what browser I am using JavaScript?

To detect user browser information we use the navigator. userAgent property. And then we match with the browser name to identify the user browser. Now call this JS function on page load, and this will display the user browser name on page load.


1 Answers

jQuery.browser can be helpful when trying to figure out which browser. jQuery.browser was removed in jQuery 1.9.

I believe it is based on navigator.UserAgent, however navigator.UserAgent can tell you the OS on its own if you want.

Try this:

var isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i) 

source

like image 88
sky-dev Avatar answered Sep 21 '22 02:09

sky-dev