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

1 Answer 1

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'
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.