jsdom: Setting a script element's `src` attribute after insertion does not load the `src` target.

If you modify a script’s src attribute after the element has been attached, the target script is never loaded.

This example will always log false to the console:

script.js

window.script_executed = true;

runner.js

const jsdom = require('jsdom');

const window = jsdom.jsdom("").defaultView;

window.script_executed = false;

const script = window.document.createElement("script");
window.document.body.appendChild(script);
script.src = "script.js";

// assume script.js will load asynchronously
setInterval(() => {
  console.log(window.script_executed);
}, 1000);

A comparable script that executes in a browser and logs true:

<html>
  <body>
    <script type="text/javascript">
      window.script_executed = false;

      var script = window.document.createElement("script");
      window.document.body.appendChild(script);
      script.src = "script.js";

      setInterval(function() {
        console.log(window.script_executed);
      }, 1000);
    </script>
  </body>
</html>

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t think further testing is helpful here. It’s clear that there are implementation bugs in resource loading with regard to attributes changing. I’ll spend some time fixing them.