Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
JavaScript can be used in nearly the same manner as using PHP or a .htaccess file. While it does have some rather obvious differences, it is still a method that you may at times want to employ. When using JavaScript you can still get the UA by calling the userAgent property of the navigator object. Listing 17.4 shows the contents of detect_js.html.
Listing 17.4 Displaying the UA with JavaScript
1: <!DOCTYPE html>
2: <html>
3: <head>
4: <title>UA with JS</title>
5: </head>
6: <body>
7: <h1>Detecting the UA with JavaScript</h1>
8: <p>Click the button below to view your device UA.</p>
9: <button type="button" onclick="getUA()">View UA</button>
10: <script>
11: function getUA() {
12: alert(navigator.userAgent);
13: }
14: </script>
15: </body>
16: </html>