Sometimes Newer Really Isn’t Better

I normally spend a good deal of time telling readers to use the latest versions of libraries because the newer versions will have additional functionality and contain bug fixes for issues that have surfaced since the library was released. There are some situations where this tactic doesn’t work and one of them appears in Chapter 2 of HTML5 Programming with JavaScript For Dummies. The example originally displayed information about the host browser, but the property used to perform that task, browser, has been deprecated in jQuery 1.9.

The easiest way to fix this problem is to use a version of jQuery that does support this property. The change is relatively small. All you need to do is change the line that reads

to read

The change will force the application to use an older version of the jQuery library. As an alternative, you can also add a call to the jQuery migrate library so that the code looks like this:

<head>
   <title>Detect a Browser</title>
   <script
   </script>
   <script
   </script>
</head>

The jQuery site recommends using feature detection instead. Although this feature is directly supported in the latest version of jQuery, there are problems with it as well. The most important issue to consider is that the site tells you outright that the library might have certain features pulled without notice or with a long deprecation cycle, which means that you code could simply stop working at some point. It’s a poor way to detect the kind of browser that you’re using because it’s unreliable. The technique shown in Chapter 2 of the book is far more reliable at the moment.

The point of this post is that there aren’t any absolutes when it comes to coding practice. You need to know when to break the rules. Let me know if you encounter any difficulties with my solution to the problem at [email protected]. If someone has a better jQuery-oriented solution to share, contact me because I’d love to hear about it.

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 117 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current offerings include topics on machine learning, AI, Android programming, and C++ programming. His technical editing skills have helped over more than 70 authors refine the content of their manuscripts. You can reach John on the Internet at [email protected]

%d bloggers like this: