How to Extend Jquery and use it along the convert._$() library.

This Article Will Help You:

 

Important

For these libraries to work, JQuery has to be enabled on the script or be included outside it.

t of 2024 that does not require JQuery.

Sample Use Case

Let say that we want to test loading new images from a different CDN of what we currently use to see if this affects conversion.

We need to set up an AB experiment in which the new CDN is reflected on all the images that we want to load from the other service. 

This is just for illustrative purposes. The code has been tested, but not in a real case scenario. It is shown here so anybody can create their own Jquery libraries that work along with Convert.

Code

The following code extends the convert version of jquery and then users in on the convert._$ wrapper library:

// Following function extends the convert jquery library
convert.$.fn.updateCdn = function() {

var imageSrc = convert.$(this).attr('src');
if(imageSrc.indexOf('https://12345.oldcn.com/') > -1) {
convert.$(this).attr('src', imageSrc.replace('https://12345.oldcn.com/', 'https://1234.newcdn.com/'));
}

};

// Following call executes the function in all of the elements returned.
convert._$('img').updateCdn();