Development

jQuery 4.0 released, first major version since 2016

Published

The jQuery team has released version 4.0, described by lead developer Timmy Willison as the “first major release in almost 10 years.” Support for the ancient IE (Internet Explorer) 10 has been dropped, and the code updated to use ECMAScript (ES) modules, but some in the developer community reckon the library is no longer needed when coding for modern web browsers.

jQuery is a small open source JavaScript library that simplifies common coding tasks, with an API that works across multiple web browsers despite incompatibilities.

Version 4.0 includes breaking changes, though Willison said most users will be able to upgrade with “minimal changes.” Some APIs have been removed because they have native equivalents in modern browsers. Support for browsers including IE 10, Edge legacy (pre-Chromium) and older versions of Firefox, Android and iOS has been dropped. These changes mean that jQuery is smaller than before, by more than 3kb with gzip compression. There is also a slim build that removes ajax modules and web animations, gaining a further 8kb reduction.

Web browsers used to disagree on the order of focus and blur events on user interface elements. Focus fires when elements receive the focus, and blur when they lose the focus. The order is complicated because in addition to blur there are three focus events: focus, focusin and focusout. Browsers used to disagree about the order in which these fired, so jQuery overrode the native behavior for cross-browser consistency. The W3C (world wide web committee) defined a specification with a different order that modern browsers now follow. jQuery follows the native behavior, which could cause unexpected results for code that relied on the overrides.

New in jQuery 4.0 is support for Trusted Types. This addresses the problem of cross-site scripting attacks where external code might be used to set insecure element properties like .innerHTML. Developers can set a content security policy that enforces trusted types, but previous versions of jQuery would violate this in some parts of the API, an issue that version 4.0 corrects.

jQuery was introduced 20 years ago by its creator John Resig, at a time when browser incompatibilities were rife. Coding asynchronous JavaScript that worked in IE, Firefox and Safari was a headache. jQuery wrapped all these problems and was immediately popular, such that developers became familiar with functions like $(“#myspan”) to get a reference to a DOM (document object model) element rather than document.getElementById(“myspan”) which performs a similar function in native JavaScript.

As a result, the library is ubiquitous. According to the w3techs survey, it is used by “88 percent of all the websites whose JavaScript library we know. This is 70.9 percent of all websites.”

This means that the jQuery team has a responsibility not to break the web, particularly as many sites retrieve the library directly from the jQuery CDN (content delivery network).

Nevertheless, there are some issues with jQuery, such that some developers recommend not using it. One is performance; using native JavaScript is generally quicker though the difference may not be noticeable. The second is that old versions stick around a long time; w3techs reckons that 19 percent of sites using jQuery still use version 2.0 which is ten years old. Third, the need for jQuery has diminished with advances in both the ECMAScript standard and cross-browser compatibility.

“We used this everywhere 15 years ago. I can’t imagine a reason to use this now if you’re writing a new web app,” said one comment on the release of version 4.0.

Views on this vary though. Although native JavaScript performs better, jQuery code is often more concise, and its API remains a pleasure to use. jQuery is the “only sane JavaScript library” according to another comment, and likely to remain embedded in the web for many years to come.