Wednesday, April 18, 2012

Detect browser info using navigator in Javascript

The following example show how to check browser info (appName, appCodeName, appVersion, platform and userAgent) using navigator in Javascript.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mobile-Web-App: Detect Browser</title>
<script type="text/javascript">

function detectbrowser(){
info = "<p><b>Browser info: </b></p>"
 + "<p>appName: " + navigator.appName + "</p>"
 + "<p>appCodeName: " + navigator.appCodeName + "</p>"
 + "<p>appVersion: " + navigator.appVersion + "</p>"
 + "<p>platform: " + navigator.platform + "</p>"
 + "<p>userAgent: " + navigator.userAgent + "</p>";

document.getElementById("browserinfo").innerHTML=info;
}

</script>
</head>
<body onload="detectbrowser();">
<h1>Mobile-Web-App: Javascript Exercise - Detect Browse</h1>
<div id="browserinfo"></div>
</body>
</html>

Screenshot of running in Opera on Linux desktop.


Screenshot of running in Chrome for Android, Beta.

No comments:

Post a Comment