jsdom.env: local jquery script doesn't work

…衆ロ難τιáo~ 提交于 2019-12-11 01:36:41

问题


  jsdom.env({
              html: "<html><body></body></html>",
              scripts: [
                //'http://code.jquery.com/jquery-1.5.min.js'
                  'http://server.local:3000/jquery/jquery.min.js'

              ]
            }, function (err, window) {

it does not work script is 'http://server.local:3000/jquery/jquery.min.js' (its available using browser) jquery.min.js is a copy of jquery-1.5.min.js

if script is 'http://code.jquery.com/jquery-1.5.min.js' - it all works.

What is wrong?


回答1:


It is not directly an answer to your question, but maybe others come to this question like I did.

You can specify local files in the scripts entry. Just put an absolute path in there or add a documentRoot entry and use a relative path. E.g.

jsdom.env({
        html: "<html><body></body></html>",
        documentRoot: __dirname + '/lib',
        scripts: [
            'jquery/jquery.min.js'
        ]
    }, function (err, window) {
    }
);



回答2:


function domify(cb){
    var file= __dirname + '/public/jquery.js';
    var jquery = fs.readFileSync(file).toString();
    jsdom.env({
        html: "<html><body></body></html>",
        src: [jquery],
        done: function (err, window) {
            cb(err,window);
        }
    });
}

this worked for me




回答3:


The question asks whether jsdom attemp to fetch a resource identified by the URL http://server.local:3000/jquery/jquery.min.js' and then execute (add the javascript functions in the fetched file (jquery.min.js) into the window object context of the jsdom DOM object. In effect, we'er trying to what a web broweser does when it would read and parse the tag and simulate the loading of jQuery library into the jsdom DOM 'window' object.

The workaround fetches the jquery.min.js library from the filesystems using Node's fs module, but not as a HTTP FETCH request. As the workaround code clearly shows, the jQuery library is read as a file into an array and then the array is passed to the jsdom function directly and not as a jsdom DOM window object.

In the jsdom documentation on GitHub, there is a snippet of code and an explanation of how to do a HTTP verb GET request from a URL into a DOM window object created by the jsdom module (see below). It follows the same pattern as an AJAX request, which is what you would expect. Yes the work around gets the jQuery library from the file system where the sever files reside. However, this is NOT an actually 'work around' to a problem in jsdom, but solves a slighly different use case where the URL is replaced by the file system reference (filename/directory) to the jQuery file.

const dom = new JSDOM(``, { url: "https://example.org/", referrer: "https://example.com/", contentType: "text/html", includeNodeLocations: true, storageQuota: 10000000 });



来源:https://stackoverflow.com/questions/8480854/jsdom-env-local-jquery-script-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!