Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Detect Browser

function fnBrowserDetect(){
                 
         let userAgent = navigator.userAgent;
         let browserName;
         
         if(userAgent.match(/chrome|chromium|crios/i)){
             browserName = "chrome";
           }else if(userAgent.match(/firefox|fxios/i)){
             browserName = "firefox";
           }  else if(userAgent.match(/safari/i)){
             browserName = "safari";
           }else if(userAgent.match(/opr//i)){
             browserName = "opera";
           } else if(userAgent.match(/edg/i)){
             browserName = "edge";
           }else{
             browserName="No browser detection";
           }
         
          document.querySelector("h1").innerText="You are using "+ browserName +" browser";         
  }
Comment

check browser

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

// Safari 3.0+ "[object HTMLElementConstructor]" 
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification));

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

// Chrome 1 - 79
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);

// Edge (based on chromium) detection
var isEdgeChromium = isChrome && (navigator.userAgent.indexOf("Edg") != -1);

// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;


var output = 'Detecting browsers by ducktyping:<hr>';
output += 'isFirefox: ' + isFirefox + '<br>';
output += 'isChrome: ' + isChrome + '<br>';
output += 'isSafari: ' + isSafari + '<br>';
output += 'isOpera: ' + isOpera + '<br>';
output += 'isIE: ' + isIE + '<br>';
output += 'isEdge: ' + isEdge + '<br>';
output += 'isEdgeChromium: ' + isEdgeChromium + '<br>';
output += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = output;
 Run code snippet
Comment

detect browser in the website

<?php

class Browser
{
    public static function detect()
    {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if ((substr($_SERVER['HTTP_USER_AGENT'], 0, 6) == "Opera/") || (strpos($userAgent, 'opera')) != false) {
            $name = 'opera';
        } elseif ((strpos($userAgent, 'chrome')) != false) {
            $name = 'chrome';
        } elseif ((strpos($userAgent, 'safari')) != false && (strpos($userAgent, 'chrome')) == false && (strpos($userAgent, 'chrome')) == false) {
            $name = 'safari';
        } elseif (preg_match('/msie/', $userAgent)) {
            $name = 'msie';
        } elseif ((strpos($userAgent, 'firefox')) != false) {
            $name = 'firefox';
        } else {
            $name = 'unrecognized';
        }
        if (preg_match('/.+(?:me|ox|it|ra|ie)[/: ]([d.]+)/', $userAgent, $matches) && $browser['name'] == 'safari') {
            $version = $matches[1];
        }
        if (preg_match('/.+(?:me|ox|it|on|ra|ie)[/: ]([d.]+)/', $userAgent, $matches) && $browser['name'] != 'safari') {
            $version = $matches[1];
        } else {
            $version = 'unknown';
        }

        return array(
            'name' => $name,
            'version' => $version,
        );
    }
}

$browser = Browser::detect();
echo 'You browser is ' . $browser['name'] . ' version ' . $browser['version'];
echo "<br />";
?>
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery get div height 
Javascript :: moment js date diff 
Javascript :: Remove all child nodes of a list: 
Javascript :: can i learn backend before frontend 
Javascript :: how to print a number with commas as thousands separators in javascript 
Javascript :: how to get aria expanded value in javascript 
Javascript :: jquery trigger 
Javascript :: mongoose count documents 
Javascript :: js array for in vs for of 
Javascript :: using .indexOf() in jShell 
Javascript :: minecraft infinite snapshot dimensions 
Javascript :: clear input from file vue 
Javascript :: remover clase jquery 
Javascript :: delete elment javascript after fade out 
Javascript :: palindrome in javascript 
Javascript :: jquery append img to div 
Javascript :: node redis json push to array 
Javascript :: convert buffer to base64 javascript 
Javascript :: angular how to check previous route 
Javascript :: firebase realtime database delete child 
Javascript :: change elements class javascript 
Javascript :: javascript json upload 
Javascript :: cloudinary download url 
Javascript :: javascript take any number of arguments 
Javascript :: how to add bootstrap to vue js 
Javascript :: jquery datatables turn off sorting 
Javascript :: react fontawesome 
Javascript :: event.preventDefault() in react hook 
Javascript :: find an object in an array of objects javascript 
Javascript :: regular expression for password 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =