how to detect mobile or desktop browser in javascript?

Today, We want to share with you javascript detecting browser.In this post we will show you get browser name and version in javascript, hear for JavaScript Detect Browser – How to Detect Browser we will give you demo and example for implement.In this post, we will learn about How to Open URL in New Tab using JavaScript? with an example.

How to detect Safari, Chrome, IE, Firefox and Opera browser?

  • Opera 8.0+
  • Safari 3.0+ “[object HTMLElementConstructor]”
  • Firefox 1.0+
  • Internet Explorer 6-11
  • Edge 20+
  • Chrome 1 – 71
  • Blink engine detection

index.html

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


var isFirefox = typeof InstallTrigger !== 'undefined';

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


var isIE = /*@[email protected]*/false || !!document.documentMode;

var isEdge = !isIE && !!window.StyleMedia;


var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);


var isBlink = (isChrome || isOpera) && !!window.CSS;


var results = 'Detecting browsers by pakainfo.com:<hr>';
results += 'isFirefox: ' + isFirefox + '<br>';
results += 'isChrome: ' + isChrome + '<br>';
results += 'isSafari: ' + isSafari + '<br>';
results += 'isOpera: ' + isOpera + '<br>';
results += 'isIE: ' + isIE + '<br>';
results += 'isEdge: ' + isEdge + '<br>';
results += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = results;

HTML Code (index.html)

<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>JavaScript Browser Detect</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="wrapper">
        <div class="onlinecheckplatform firefox"></div>
        <div class="onlinecheckplatform chrome"></div>
        <div class="onlinecheckplatform safari"></div>
        <div class="onlinecheckplatform edge"></div>
    </div>

    <script src="script.js"></script>
</body>
</html>

CSS Code

.wrapper {
    position: absolute;
    left: 50vw;
    top: 50vh;
    transform: translateX(-50%) translateY(-50%);
}
.wrapper .onlinecheckplatform {
    display: inline-block;
    height: 70px;
    width: 70px;
    filter: grayscale(100%);
    position: relative;
    background-repeat: no-repeat;
    background-size: 70px 70px;
}
.wrapper .firefox {
  background-image: url('icon/firefox.png');
}
.wrapper .chrome {
  background-image: url('icon/chrome.png');
}
.wrapper .safari {
  background-image: url('icon/safari.png');
}
.wrapper .edge {
  background-image: url('icon/edge.png');
}
.wrapper .onlinecheckplatform.active {
    filter: grayscale(0%);
    animation-duration: 1s;
    animation-name: highlight;
    animation-timing-function: ease-out;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}
@keyframes highlight {
    from {
        top: 0;
    }
    to {
        top: -20px;
    }
}

JavaScript Code: script.js

var onlinecheckplatform;
var agent = navigator.userAgent.match(/(opera|chrome|safari|firefox|msie)/?s*(.?d+(.d+)*)/i);
if (navigator.userAgent.match(/Edge/i) || navigator.userAgent.match(/Trident.*rv[ :]*11./i)) {
    onlinecheckplatform = "msie";
}
else {
    onlinecheckplatform = agent[1].toLowerCase();
}
if (document.getElementsByClassName(onlinecheckplatform).length > 0)
    document.getElementsByClassName(onlinecheckplatform)[0].classList.add('active');

jQuery Detect Browser

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

// JQuery code to detect browser
$(document).ready(function() {
    var onlinecheckplatform;
    var agent = navigator.userAgent.match(/(opera|chrome|safari|firefox|msie)/?s*(.?d+(.d+)*)/i);
    if (navigator.userAgent.match(/Edge/i) || navigator.userAgent.match(/Trident.*rv[ :]*11./i)) {
        onlinecheckplatform = "edge";
    }
    else {
        onlinecheckplatform = agent[1].toLowerCase();
    }
    $('.onlinecheckplatform.' + onlinecheckplatform).addClass("active");
});

I hope you get an idea about npm detect-browser.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.