Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with jQuery and remain agnostic to its version?

We're developing a snippet that is embedded in 3rd party sites, and uses jQuery.

The snippet checks if the site already has jQuery loaded. If so, it uses that library, otherwise it loads our own copy (currently jQuery 1.4.4, we're migrating to the latest soon).

There are breaking changes in jQuery. One such change, for example is the change in semantics of attr(), and introduction of prop().

Are there some guidelines to working with jQuery in a way that will be as backward compatible as possible? Even when we migrate to latest, we still want to use an existing jQuery library if it exists instead of loading a new copy, to save load time and resources.

like image 227
ripper234 Avatar asked Jan 25 '12 14:01

ripper234


2 Answers

There are a few solutions to your problem

1. Use a subset of jQuery

Find the subset of jQuery that works upto a reasonable amount back and only use that subset.

Note that I recommend not using .attr at all, ever. since a) it's a minefield and b) it has different behaviour on different versions. You may find the sensible subset of jQuery is quite small. Good luck finding it.

2. Just use the latest version

Use the latest version of jQuery and tell your library users that they need to upgrade.

Seriously everyone should be using the latest version anyway, force them to upgrade.

3. Don't use jQuery

Don't have a dependency on jQuery.

Seriously, the less dependencies you have the better your code is

I prefer 3. as an optimum solution

like image 156
3 revs Avatar answered Sep 20 '22 17:09

3 revs


Take a look at these guidelines: http://www.davidtong.me/upgrading-jquery-from-1-4-x-to-1-6-1/

it's not the latest version available of jQuery, but it's enough hot.

Version 1.6.4 is a minor release

From version 1.6.4 to 1.7.x some new features and fixes were introduced but nothing seems is breaking code written for jQuery 1.6 version

like image 42
Fabrizio Calderan Avatar answered Sep 17 '22 17:09

Fabrizio Calderan