【问题标题】:jsdom can't load local html and javascriptjsdom无法加载本地html和javascript
【发布时间】:2018-04-19 16:08:34
【问题描述】:

感谢您的帮助。

使用jsdom,我正在尝试加载一个本地 HTML 文件,该文件本身会加载一个本地 JS 文件。

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 本身来源 helloworld.js

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

但是我无法访问或读取helloworld.js 中的任何变量

问候,山姆

【问题讨论】:

    标签: jsdom


    【解决方案1】:

    您需要添加如下所示的 runsScripts 和资源属性

    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'
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      相关资源
      最近更新 更多