Techumber

Simple JavaScript To Detect Browser In Desktop And Mobile Phones

Github avatar
Dimpu
January 16, 2014. 1 min read

Just a simple javascript code to detect you browser in all most all devices. There is nothing much to explain only thing is we will use window.navigator and **window.navigator.userAgent  **to do this.

index.html

<html lang="en"> 
<head> 
<title>Detect Browser Using JavaScript</title> 
</head> 
<body> 
<script src="detect.js" type="text/javascript"></script> 
</body> 
</html> 

detect.js

var BrowserDetect = function() { 
var nav = window.navigatorundefined 
ua = window.navigator.userAgent.toLowerCase(); 
// detect browsers (only the ones that have some kind of quirk we need to work around) 
if (ua.match(/ipad/i) !== null) 
return "iPod"; 
if (ua.match(/iphone/i) !== null) 
return "iPhone"; 
if (ua.match(/android/i) !== null) 
return "Android"; 
if ((nav.appName.toLowerCase().indexOf("microsoft") != -1 || nav.appName.toLowerCase().match(/trident/gi) !== null)) 
return "IE"; 
if (ua.match(/chrome/gi) !== null) 
return "Chrome"; 
if (ua.match(/firefox/gi) !== null) 
return "Firefox"; 
if (ua.match(/webkit/gi) !== null) 
return "Webkit"; 
if (ua.match(/gecko/gi) !== null) 
return "Gecko"; 
if (ua.match(/opera/gi) !== null) 
return "Opera"; 
//If any case miss we will return null 
return null 
} 
document.write(BrowserDetect()); 

...