How to know which version of the browser dynamically?

Asked

Viewed 7,365 times

1

I’m doing a project in CORDOVA using basically HTML, CSS, JS. But in some mobile phones the image of some icons are getting their natural size and in others not, I have used basically vh to set the size of the images. To test this I want to check which version of the browser is open, to check whether there is portability or not.

Would you have some javascript/jquery command that would do this ?

  • 1

    Fala Renan, caso queria existe um plugin jquery para isso https://github.com/gabceb/jquery-browser-plugin the advantage of it is that it has the unification of various detection standards of browsers from mobile in different Sos returning the most accurate information possible.

  • http://www.whatbrowser.org/intl/pt-BR/ Easy Muuuuito... Copy and paste the code that gugãl created.....

2 answers

7

You can detect which browser is being used by the user as follows: Jsfiddle

    // Opera 8.0+
var opera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    // Firefox 1.0+
var firefox = typeof InstallTrigger !== 'undefined';
    // At least Safari 3+: "[object HTMLElementConstructor]"
var safari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
    // Internet Explorer 6-11
var ie = /*@cc_on!@*/false || !!document.documentMode;
    // Edge 20+
var iedge = !isIE && !!window.StyleMedia;
    // Chrome 1+
var chrome = !!window.chrome && !!window.chrome.webstore;
    // Blink engine detection
var blink = (isChrome || isOpera) && !!window.CSS;

To detect which browser and its respective version, you can use the following function, where an object will be returned with the browser name, its version and other browser information: Jsfiddle

function get_browser(){
    var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 
    if(/trident/i.test(M[1])){
        tem=/\brv[ :]+(\d+)/g.exec(ua) || []; 
        return {name:'IE',version:(tem[1]||'')};
        }   
    if(M[1]==='Chrome'){
        tem=ua.match(/\bOPR\/(\d+)/)
        if(tem!=null)   {return {name:'Opera', version:tem[1]};}
        }   
    M=M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);}
    return {
      name: M[0],
      version: M[1]
    };
 }

6


In javascript is very simple:

alert(navigator.appVersion);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.