Information Technology Reference
In-Depth Information
Figure 6-4: Drat! Looks like this person isn't using an Android browser!
JavaScript User Agent Detection
While PHP is great for detecting the user agent string on the server's side, there might
be times when a developer would need to detect the User String on the client side. As
an example, let's say you are working on a site or application that gives you the ability to
export information into either a text file or binary format file on the user's computer for
viewing later. On an Android device, the text is no problem but if your user chooses the
binary format, they might be confused when it can't be opened. Thus, we may just want
to hide that option, leaving the other available. Luckily, using JavaScript for this task is
just as easy as it was using PHP.
Introducing the JavaScript Agent Detection Code
The JavaScript code shown in Listing 6-2 gives us a quick taste of how simple
detection in JavaScript can be; it is similar to PHP. The only major difference in this
code, when compared to the last, is that instead of writing our message to the page
like we did with PHP, we are instead opting to show our users a popup alert dialog
(see Figures 6-5 and 6-6).
 
Search WWH ::




Custom Search