Created
August 28, 2020 11:50
-
-
Save utopianfool/2a05c4b0554dea8d44b369696a96a120 to your computer and use it in GitHub Desktop.
Detect user's browser and run related example functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Detect Browsers functions | |
*/ | |
/* Get IE or Edge browser version */ | |
var version = detectIE(); | |
if (version === false) { | |
document.getElementById('result').innerHTML = '<s>IE/Edge</s>'; | |
} else if (version >= 12) { | |
document.getElementById('result').innerHTML = 'Edge ' + version; | |
} else { | |
document.getElementById('result').innerHTML = 'IE ' + version; | |
} | |
// add details to debug result | |
document.getElementById('details').innerHTML = window.navigator.userAgent; | |
/** | |
* detect IE | |
* returns version of IE or false, if browser is not Internet Explorer | |
*/ | |
function detectIE() { | |
var ua = window.navigator.userAgent; | |
// Test values; Uncomment to check result … | |
// IE 10 | |
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'; | |
// IE 11 | |
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; | |
// Edge 12 (Spartan) | |
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'; | |
// Edge 13 | |
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'; | |
var msie = ua.indexOf('MSIE '); | |
if (msie > 0) { | |
// IE 10 or older => return version number | |
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10); | |
} | |
var trident = ua.indexOf('Trident/'); | |
if (trident > 0) { | |
// IE 11 => return version number | |
var rv = ua.indexOf('rv:'); | |
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10); | |
} | |
var edge = ua.indexOf('Edge/'); | |
if (edge > 0) { | |
// Edge (IE 12+) => return version number | |
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10); | |
} | |
// other browser | |
return false; | |
} | |
/* Sample functions that returns boolean in case the browser is Internet Explorer*/ | |
function isIE() { | |
ua = navigator.userAgent; | |
/* MSIE used to detect old browsers and Trident used to newer ones*/ | |
var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1; | |
return is_ie; | |
} | |
/* Create an alert to show if the browser is IE or not */ | |
if (isIE()){ | |
alert('It is InternetExplorer'); | |
}else{ | |
alert('It is NOT InternetExplorer'); | |
} | |
function BrowserDetection() { | |
//Check if browser is IE | |
if (navigator.userAgent.search("MSIE") & gt; = 0) { | |
// insert conditional IE code here | |
} | |
//Check if browser is Chrome | |
else if (navigator.userAgent.search("Chrome") & gt; = 0) { | |
// insert conditional Chrome code here | |
} | |
//Check if browser is Firefox | |
else if (navigator.userAgent.search("Firefox") & gt; = 0) { | |
// insert conditional Firefox Code here | |
} | |
//Check if browser is Safari | |
else if (navigator.userAgent.search("Safari") & gt; = 0 & amp; & amp; navigator.userAgent.search("Chrome") & lt; 0) { | |
// insert conditional Safari code here | |
} | |
//Check if browser is Opera | |
else if (navigator.userAgent.search("Opera") & gt; = 0) { | |
// insert conditional Opera code here | |
} | |
} | |
function msieversion() { | |
var ua = window.navigator.userAgent; | |
var msie = ua.indexOf("MSIE "); | |
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number | |
{ | |
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)))); | |
} | |
else // If another browser, return 0 | |
{ | |
alert('otherbrowser'); | |
} | |
return false; | |
} | |
/** | |
* https://stackoverflow.com/questions/13478303/correct-way-to-use-modernizr-to-detect-ie | |
* https://stackoverflow.com/questions/19999388/check-if-user-is-using-ie | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment