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 John@JohnMuellerBooks.com. 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 99 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 books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.