,

Testing against multiple jQuery versions

I recently took over maintenance of a jQuery UI plugin that lets you highlight bits of a textarea. Since it’s a UI plugin, most of the testing is done by visual inspection, and supporting different versions of jQuery presents an interesting testing challenge, since jQuery is one of the first things to load on the page.

jquery-highlight

This is the code that generates the above example:

function runTest() {
  $('#demo1').highlightTextarea({
      words: {
        color: '#ADF0FF',
        words: ['Lorem ipsum','vulputate']
      },
      debug: true
  });
}

It turns out that you can dynamically add scripts to a page, so rather than including jQuery in a script tag, we can import it after the page loads.

Normally you might use “$(document).ready” to run initialization code, but you can use window.onload directly instead:

window.onload = loadJavascript;

Inside the runTests function, you need the ability to append a script, and call a callback once it loads, since this is an asynchronous process.

function appendScript(src, callback) {
  var head = document.getElementsByTagName('head')[0];
 
  var elt = document.createElement("script");
  elt.type = "text/javascript";
  elt.src = src;
 
  elt.onload = function() {
    callback();
  }
 
  head.appendChild(elt);
}

To make the page load correctly, I’ve set this up so you can specify the jQuery version on the URL. Once jQuery loads, it triggers jQuery UI loading, and then finally our own scritpts.

function loadJavascript() {
  // this sets the default
  var jqv = "jquery-1.11.1.min.js";
 
  try {
    jqv = window.location.search.split('?')[1].split('=')[1];
  } catch (e) {}
 
  appendScript("http://code.jquery.com/" + jqv,
    function() {
      $('#jqv').val(jqv);
 
      appendScript(
        "http://code.jquery.com/ui/1.10.4/jquery-ui.min.js",
      function() {
        appendScript("../jquery.highlighttextarea.js",
        runTest
      )})});
}

Once this works, you can add a dropdown with all the jQuery versions you want to support, and set the page to refresh when one is selected:

function setJQuery(newVersion) {
  var jqv = $('#jqv').val();
 
  window.location = 'index.html?jqv=' + jqv;
}

For completeness, here is the dropdown:

<select id="jqv" onchange="setJQuery()">
  <option value="jquery-2.2.0.js">jQuery Core 2.2.0</option>
  <option value="jquery-2.1.4.js">jQuery Core 2.1.4</option>
  <option value="jquery-2.1.3.js">jQuery Core 2.1.3</option>
  <option value="jquery-2.1.2.js">jQuery Core 2.1.2</option>
  <option value="jquery-2.1.1.js">jQuery Core 2.1.1</option>
  <option value="jquery-2.1.1-rc2.js">jQuery Core 2.1.1-rc2</option>
  <option value="jquery-2.1.1-rc1.js">jQuery Core 2.1.1-rc1</option>
  <option value="jquery-2.1.1-beta1.js">jQuery Core 2.1.1-beta1</option>
  <option value="jquery-2.1.0.js">jQuery Core 2.1.0</option>
  <option value="jquery-2.1.0-rc1.js">jQuery Core 2.1.0-rc1</option>
  <option value="jquery-2.1.0-beta3.js">jQuery Core 2.1.0-beta3</option>
  <option value="jquery-2.1.0-beta2.js">jQuery Core 2.1.0-beta2</option>
  <option value="jquery-2.1.0-beta1.js">jQuery Core 2.1.0-beta1</option>
  <option value="jquery-2.0.3.js">jQuery Core 2.0.3</option>
  <option value="jquery-2.0.2.js">jQuery Core 2.0.2</option>
  <option value="jquery-2.0.1.js">jQuery Core 2.0.1</option>
  <option value="jquery-2.0.0.js">jQuery Core 2.0.0</option>
  <option value="jquery-2.0.0-beta3.js">jQuery Core 2.0.0-beta3</option>
  <option value="jquery-2.0.0b2.js">jQuery Core 2.0.0-b2</option>
  <option value="jquery-2.0.0b1.js">jQuery Core 2.0.0-b1</option>
  <option value="jquery-1.12.0.js">jQuery Core 1.12.0</option>
  <option value="jquery-1.11.3.js">jQuery Core 1.11.3</option>
  <option value="jquery-1.11.2.js">jQuery Core 1.11.2</option>
  <option value="jquery-1.11.1.js">jQuery Core 1.11.1</option>
  <option value="jquery-1.11.1-rc2.js">jQuery Core 1.11.1-rc2</option>
  <option value="jquery-1.11.1-rc1.js">jQuery Core 1.11.1-rc1</option>
  <option value="jquery-1.11.1-beta1.js">jQuery Core 1.11.1-beta1</option>
  <option value="jquery-1.11.0.js">jQuery Core 1.11.0</option>
  <option value="jquery-1.11.0-rc1.js">jQuery Core 1.11.0-rc1</option>
  <option value="jquery-1.11.0-beta3.js">jQuery Core 1.11.0-beta3</option>
  <option value="jquery-1.11.0-beta2.js">jQuery Core 1.11.0-beta2</option>
  <option value="jquery-1.11.0-beta1.js">jQuery Core 1.11.0-beta1</option>
  <option value="jquery-1.10.2.js">jQuery Core 1.10.2</option>
  <option value="jquery-1.10.1.js">jQuery Core 1.10.1</option>
  <option value="jquery-1.10.0.js">jQuery Core 1.10.0</option>
  <option value="jquery-1.10.0-beta1.js">jQuery Core 1.10.0-beta1</option>
  <option value="jquery-1.9.1.js">jQuery Core 1.9.1</option>
  <option value="jquery-1.9.0.js">jQuery Core 1.9.0</option>
  <option value="jquery-1.9.0rc1.js">jQuery Core 1.9.0-rc1</option>
  <option value="jquery-1.9.0b1.js">jQuery Core 1.9.0-b1</option>
  <option value="jquery-1.8.3.js">jQuery Core 1.8.3</option>
  <option value="jquery-1.8.2.js">jQuery Core 1.8.2</option>
  <option value="jquery-1.8.1.js">jQuery Core 1.8.1</option>
  <option value="jquery-1.8.0.js">jQuery Core 1.8.0</option>
  <option value="jquery-1.8rc1.js">jQuery Core 1.8.0-rc1</option>
  <option value="jquery-1.8b2.js">jQuery Core 1.8.0-b2</option>
  <option value="jquery-1.8b1.js">jQuery Core 1.8.0-b1</option>
  <option value="jquery-1.7.2.js">jQuery Core 1.7.2</option>
  <option value="jquery-1.7.2rc1.js">jQuery Core 1.7.2-rc1</option>
  <option value="jquery-1.7rc1.js">jQuery Core 1.7.0-rc1</option>
  <option value="jquery-1.7b2.js">jQuery Core 1.7.0-b2</option>
  <option value="jquery-1.7b1.js">jQuery Core 1.7.0-b1</option>
  <option value="jquery-1.6.4.js">jQuery Core 1.6.4</option>
  <option value="jquery-1.6.4rc1.js">jQuery Core 1.6.4-rc1</option>
  <option value="jquery-1.6.3.js">jQuery Core 1.6.3</option>
  <option value="jquery-1.6.3rc1.js">jQuery Core 1.6.3-rc1</option>
  <option value="jquery-1.6.2.js">jQuery Core 1.6.2</option>
  <option value="jquery-1.6.2rc1.js">jQuery Core 1.6.2-rc1</option>
  <option value="jquery-1.6.1.js">jQuery Core 1.6.1</option>
  <option value="jquery-1.6.1rc1.js">jQuery Core 1.6.1-rc1</option>
  <option value="jquery-1.6.js">jQuery Core 1.6.0</option>
  <option value="jquery-1.6rc1.js">jQuery Core 1.6.0-rc1</option>
  <option value="jquery-1.6b1.js">jQuery Core 1.6.0-b1</option>
  <option value="jquery-1.5.2.js">jQuery Core 1.5.2</option>
  <option value="jquery-1.5.2rc1.js">jQuery Core 1.5.2-rc1</option>
  <option value="jquery-1.5.1.js">jQuery Core 1.5.1</option>
  <option value="jquery-1.5.1rc1.js">jQuery Core 1.5.1-rc1</option>
  <option value="jquery-1.5.js">jQuery Core 1.5.0</option>
  <option value="jquery-1.5rc1.js">jQuery Core 1.5.0-rc1</option>
  <option value="jquery-1.5b1.js">jQuery Core 1.5.0-b1</option>
  <option value="jquery-1.4.4.js">jQuery Core 1.4.4</option>
  <option value="jquery-1.4.4rc3.js">jQuery Core 1.4.4-rc3</option>
  <option value="jquery-1.4.4rc2.js">jQuery Core 1.4.4-rc2</option>
  <option value="jquery-1.4.4rc1.js">jQuery Core 1.4.4-rc1</option>
  <option value="jquery-1.4.3.js">jQuery Core 1.4.3</option>
  <option value="jquery-1.4.3rc2.js">jQuery Core 1.4.3-rc2</option>
  <option value="jquery-1.4.3rc1.js">jQuery Core 1.4.3-rc1</option>
  <option value="jquery-1.4.2.js">jQuery Core 1.4.2</option>
  <option value="jquery-1.4.1.js">jQuery Core 1.4.1</option>
  <option value="jquery-1.4.js">jQuery Core 1.4.0</option>
  <option value="jquery-1.4rc1.js">jQuery Core 1.4.0-rc1</option>
  <option value="jquery-1.4a2.js">jQuery Core 1.4.0-a2</option>
  <option value="jquery-1.4a1.js">jQuery Core 1.4.0-a1</option>
  <option value="jquery-1.3.2.js">jQuery Core 1.3.2</option>
  <option value="jquery-1.3.1.js">jQuery Core 1.3.1</option>
  <option value="jquery-1.3.1rc1.js">jQuery Core 1.3.1-rc1</option>
  <option value="jquery-1.3.js">jQuery Core 1.3.0</option>
  <option value="jquery-1.3rc2.js">jQuery Core 1.3.0-rc2</option>
  <option value="jquery-1.3rc1.js">jQuery Core 1.3.0-rc1</option>
  <option value="jquery-1.3b2.js">jQuery Core 1.3.0-b2</option>
  <option value="jquery-1.3b1.js">jQuery Core 1.3.0-b1</option>
  <option value="jquery-1.2.6.js">jQuery Core 1.2.6</option>
  <option value="jquery-1.2.5.js">jQuery Core 1.2.5</option>
  <option value="jquery-1.2.4.js">jQuery Core 1.2.4</option>
  <option value="jquery-1.2.4b.js">jQuery Core 1.2.4-b</option>
  <option value="jquery-1.2.4a.js">jQuery Core 1.2.4-a</option>
  <option value="jquery-1.2.3.js">jQuery Core 1.2.3</option>
  <option value="jquery-1.2.3b.js">jQuery Core 1.2.3-b</option>
  <option value="jquery-1.2.3a.js">jQuery Core 1.2.3-a</option>
  <option value="jquery-1.2.3.js">jQuery Core 1.2.3</option>
  <option value="jquery-1.2.3b.js">jQuery Core 1.2.3-b</option>
  <option value="jquery-1.2.3a.js">jQuery Core 1.2.3-a</option>
  <option value="jquery-1.2.2.js">jQuery Core 1.2.2</option>
  <option value="jquery-1.2.2b2.js">jQuery Core 1.2.2-b2</option>
  <option value="jquery-1.2.2b.js">jQuery Core 1.2.2-b</option>
  <option value="jquery-1.2.1.js">jQuery Core 1.2.1</option>
  <option value="jquery-1.2.js">jQuery Core 1.2.0</option>
  <option value="jquery-1.1.4.js">jQuery Core 1.1.4</option>
  <option value="jquery-1.1.3.js">jQuery Core 1.1.3</option>
  <option value="jquery-1.1.3a.js">jQuery Core 1.1.3-a</option>
  <option value="jquery-1.1.2.js">jQuery Core 1.1.2</option>
  <option value="jquery-1.1.1.js">jQuery Core 1.1.1</option>
  <option value="jquery-1.1.js">jQuery Core 1.1.0</option>
  <option value="jquery-1.1b.js">jQuery Core 1.1.0-b</option>
  <option value="jquery-1.1a.js">jQuery Core 1.1.0-a</option>
  <option value="jquery-1.0.4.js">jQuery Core 1.0.4</option>
  <option value="jquery-1.0.3.js">jQuery Core 1.0.3</option>
  <option value="jquery-1.0.2.js">jQuery Core 1.0.2</option>
  <option value="jquery-1.0.1.js">jQuery Core 1.0.1</option>
  <option value="jquery-1.0.js">jQuery Core 1.0.0</option>
</select>

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *