Download qUnit asynchronously

I am trying to load QUnit in js, but the addevent function in QUnit.js never starts and it just doesn't work:

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

You can use jquery getScript, for 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 a callback to place your code that processes the new loaded js code.

+2
source

You may also need to call QUnit.load () to initialize QUnit:

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

:

void(function foo()
  {
  /* get head element */
  var head=document.getElementsByTagName("head")[0];

  /* create script and link elements */
  var qUnitJS = document.createElement("script");
  var qUnitCSS = document.createElement("link");

  /* link rel and type attributes required for lazy loading */
  qUnitCSS.rel="stylesheet";
  qUnitCSS.type="text/css";

  /* define script src attribute to lazy load */
  qUnitJS.src = "http://qunitjs.com/resources/qunit.js";

  /* append script and link elements */
  head.appendChild(qUnitJS);
  head.appendChild(qUnitCSS);

  /* define link href after DOM insertion to lazy load */
  qUnitCSS.href="http://qunitjs.com/resources/qunit.css";

  /* call tests after QUnit loads */
  qUnitJS.onload = function () {};
  }() )

Bookmarklet

javascript:void(function foo(){var head=document.getElementsByTagName("head")[0]; var qUnitJS = document.createElement("script"); var qUnitCSS = document.createElement("link"); qUnitCSS.rel="stylesheet"; qUnitCSS.type="text/css"; qUnitJS.src = "http://qunitjs.com/resources/qunit.js"; head.appendChild(qUnitJS); head.appendChild(qUnitCSS); qUnitCSS.href="http://qunitjs.com/resources/qunit.css"; qUnitJS.onload = function () {};}() )

Firefox security.mixed_content.block_active_content false about:config, .

0

Source: https://habr.com/ru/post/1746306/


All Articles