Search code examples
javascriptjqueryasynchronousdependency-injectionqunit

load qUnit asynchronously


I am trying to load QUnit in js but the addevent function in QUnit.js is never fired, and it's just not working:

var appendQUnit = document.createElement('script'); 
appendQUnit.src = 'js/utility/qunit/qunit.js';
appendQUnit.type = 'text/javascript'; 
document.getElementsByTagName('head')[0].appendChild(appendQUnit); 

Solution

  • You can use the jquery's getScript, example:

    $.getScript('js/utility/qunit/qunit.js', function() {
        // here you can handle the qunit code
    });
    

    because the browser will always load javascript files in async mode, you will need some callback to place your code that handles the new loaded js code.