Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 29, 2023 03:18
Show Gist options
  • Save code-boxx/3875db284f522cacc375da94150edaf2 to your computer and use it in GitHub Desktop.
Save code-boxx/3875db284f522cacc375da94150edaf2 to your computer and use it in GitHub Desktop.
Javascript Detect Browser

JAVASCRIPT DETECT BROWSER

https://code-boxx.com/detect-browser-with-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>User Agent Detection</title>
<script>
window.addEventListener("load", () => {
// CHROME
if (navigator.userAgent.indexOf("Chrome") != -1 ) {
console.log("Google Chrome");
}
// FIREFOX
else if (navigator.userAgent.indexOf("Firefox") != -1 ) {
console.log("Mozilla Firefox");
}
// INTERNET EXPLORER
else if (navigator.userAgent.indexOf("MSIE") != -1 ) {
console.log("Internet Exploder");
}
// EDGE
else if (navigator.userAgent.indexOf("Edge") != -1 ) {
console.log("Internet Exploder");
}
// SAFARI
else if (navigator.userAgent.indexOf("Safari") != -1 ) {
console.log("Safari");
}
// OPERA
else if (navigator.userAgent.indexOf("Opera") != -1 ) {
console.log("Opera");
}
// YANDEX BROWSER
else if (navigator.userAgent.indexOf("YaBrowser") != -1 ) {
console.log("YaBrowser");
}
// OTHERS
else {
console.log("Others");
}
});
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Bowser Library</title>
<!-- (A) LOAD BOWSER -->
<!-- https://cdnjs.com/libraries/bowser -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bowser/2.11.0/bundled.js"></script>
<script>
// (B) DETECT BROWSER
// https://github.com/lancedikson/bowser
// https://www.jsdelivr.com/package/npm/bowser
window.addEventListener("load", () => {
// (B1) PARSE USER AGENT
var result = bowser.getParser(navigator.userAgent).getResult();
// (B2) BROWSER INFO
console.log(result.browser.name);
console.log(result.browser.version);
console.log(result.engine);
// (B3) OPERATING SYSTEM
console.log(result.os.name);
console.log(result.os.version);
console.log(result.os.versionName);
// (B4) PLATFORM
console.log(result.platform.type);
});
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Prefix Detection</title>
<script>
// CREDITS : https://davidwalsh.name/vendor-prefix
window.addEventListener("load", () => {
var prefix = (Array.prototype.slice
.call(window.getComputedStyle(document.documentElement, ""))
.join("")
.match(/-(moz|webkit|ms)-/))[1];
// MOZ - FIREFOX (GECKO ENGINE)
// WEBKIT - CHROME, SAFARI, OPERA (WEBKIT ENGINE)
// MS - INTERNET EXPLORER & EDGE (TRIDENT ENGINE)
// NOTE - OLD OPERA VERSIONS USE PRESTO ENGINE. PREFIX IS -O
console.log(prefix);
});
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Prefix Detection</title>
<script>
// CREDITS https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser/9851769
window.addEventListener("load", () => {
// 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+
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
// INTERNET EXPLORER 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// EDGE 20+
var isEdge = !isIE && !!window.StyleMedia;
// CHROME 1+
var isChrome = !!window.chrome;
// BLINK ENGINE DETECTION
var isBlink = (isChrome || isOpera) && !!window.CSS;
console.log("Opera - " + isOpera);
console.log("Firefox - " + isFirefox);
console.log("Safari - " + isSafari);
console.log("IE - " + isIE);
console.log("Edge - " + isEdge);
console.log("Chrome - " + isChrome);
console.log("Blink - " + isBlink);
});
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment