Js to determine the type and version of the browser code

  • 2020-03-30 02:56:04
  • OfStack

Phpnew blog built-in discuz ubb type editor, editor function has been inserted at the cursor, but in ie11 has been inaccurate.
Today, I analyzed it and finally found the reason. The browser judged that there was an exception to the js file written by the old version, so the insertion point was always in the header. Today, I fixed it.

Provides a js function. Returns an array.


function sys_userAgent(){ 
    var userAgent = navigator.userAgent,     
    rMsie = /(msies|trident.*rv:)([w.]+)/,     
    rFirefox = /(firefox)/([w.]+)/,     
    rOpera = /(opera).+version/([w.]+)/,     
    rChrome = /(chrome)/([w.]+)/,     
    rSafari = /version/([w.]+).*(safari)/;    
    var browser,version,ua;    
    ua = userAgent.toLowerCase();    
    var match = rMsie.exec(ua);    
    if (match != null) {    
        return { browser : "ie", version : match[2] || "0" };    
    } 

    if (!!window.ActiveXObject || "ActiveXObject" in window){ 
        return { browser : "ie", version : "0"};     
    } 

    var match = rFirefox.exec(ua);    
    if (match != null) {    
        return { browser : "firefox", version : match[2] || "0" };    
    }    
    var match = rOpera.exec(ua);    
    if (match != null) {    
        return { browser : "opera", version : match[2] || "0" };    
    }    
    var match = rChrome.exec(ua);    
    if (match != null) {    
        return { browser : "chrome", version : match[2] || "0" };    
    }    
    var match = rSafari.exec(ua);    
    if (match != null) {    
        return { browser : "safari", version : match[1] || "0" };    
    }    
    if (match != null) {    
        return { browser : "", version : "0" };    
    } 
} 


Related articles: