There are many cases in which we need to get exact information about the client on which the code we wrote in JavaScript is running. Getting the information we need out of the user-agent is not straight forward. Using the UAParser.js lightweight JavaScript library it becomes much simpler.

The following demo shows how simple it is to us this library in order to get the exact information we need out of the user-agent.

<!DOCTYPE html>
<html>
<head>
    <title>UAParser.js Demo</title>
    <script type="text/javascript" src="ua-parser.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        var parser = new UAParser();
        var result = parser.getResult();
        document.writeln("<br>browser.name="+result.browser.name);
        document.writeln("<br>browser.major="+result.browser.major);
        document.writeln("<br>browser.version="+result.browser.version);
        document.writeln("<br>os.name="+result.os.name);
        document.writeln("<br>os.version="+result.os.version);
        document.writeln("<br>engine.name="+result.engine.name);
        document.writeln("<br>cpu.architecture="+result.cpu.architecture);
    </script>
</body>
</html>

The following video clip overviews this code sample, shows its execution and explains it.

You can find more code samples, slides and video clips for learning this library in my free online courses website at http://abelski.lifemichael.com.

Leave a Reply

Your email address will not be published. Required fields are marked *