0

Thanks for any help.

Using jsdom, I'm trying to load a local HTML file, which itself loads a local JS file.

import jsdom from 'jsdom-no-contextify'

var fs = require('fs');
var path = require("path");

var html = fs.readFileSync(path.join(__dirname, '../src/', 'launcher.html'));

global.document = jsdom.jsdom(html, {
    FetchExternalResources: ['script'],
    ProcessExternalResources: ['script'],
    created: function (error, window) {
        console.log("created: " + error);
    },
    url: "file://mydir/src/js/helloworld.js"
});

global.window = document.parentWindow;

window.addEventListener('load', function () {
});

launcher.html itself sources helloworld.js i.e.

<script type="text/javascript" src="js/helloworld.js"></script>

However I can't access or read any variables inside helloworld.js

Regards, Sam

Jack
  • 10,313
  • 15
  • 75
  • 118
Sam
  • 1
  • 4

1 Answers1

0

you need to add the runsScripts and resources properties as below

global.document = jsdom.jsdom(html, {
    FetchExternalResources: ['script'],
    ProcessExternalResources: ['script'],
    created: function (error, window) {
        console.log("created: " + error);
    },
    url: "file://mydir/src/js/helloworld.js",
    runScripts: "dangerously",
    resources:'usable'
});