我是nodejs的新手.我想使用jsdom来解析一些可能包含脚本代码的网页.但我得到错误表示函数或变量未定义.任何人都可以给出一些指示.
我的代码
var jsdom = require('jsdom');
jsdom.env({
html: 'http://10.200.0.10:8080/test/a.html',scripts: [
'http://code.jquery.com/jquery-1.5.min.js'
],done: function(errors,window) {
var $= window.$;
window.onload();
console.log(window.a);
}
});
和HTML页面在这里
并在下面收到错误消息
dfddddfdf
undefined:1: ReferenceError: loads is not defined
loads()
^
ReferenceError: loads is not defined
at unknown source
at /root/node_modules/jsdom/node_modules/contextify/lib/contextify.js:10:24
at /root/node_modules/jsdom/lib/jsdom/level1/core.js:1024:50
at /root/testnode.js:18:12
at Array.0 (/root/node_modules/jsdom/lib/jsdom.js:199:39)
at EventEmitter._tickCallback (node.js:192:40)
node.js:201
throw e; // process.nextTick error,or 'error' event on first tick
^
ReferenceError: loads is not defined
at unknown source
at /root/node_modules/jsdom/node_modules/contextify/lib/contextify.js:10:24
at /root/node_modules/jsdom/lib/jsdom/level1/core.js:1024:50
at /root/testnode.js:18:12
at Array.0 (/root/node_modules/jsdom/lib/jsdom.js:199:39)
at EventEmitter._tickCallback (node.js:192:40)
它报告未定义加载函数,但它实际上是在网页中声明的.
任何人都可以给出一些建议,或者只是告诉我jsdom无法处理页面中嵌入的脚本.
最佳答案
这是您想要的代码:
var fs = require('fs');
var jsdom = require('jsdom');
var doc = jsdom.jsdom(fs.readFileSync("a.html"),null,{
features: {
FetchExternalResources : ['script'],ProcessExternalResources : ['script'],MutationEvents : '2.0',}
});
var window = doc.createWindow();
jsdom.jQueryify(window,"http://code.jquery.com/jquery-1.5.min.js",function() {
console.log(window.a);
console.log(window.$().jquery); //jquery version
});