admin管理员组

文章数量:1278910

I am new to nodejs. I want to use jsdom to parse some web pages which may contain script code inside. But I got error said the function or variable was not defined. Can anyone give some directions about this.

my code

var jsdom = require('jsdom');

jsdom.env({
  html: 'http://10.200.0.10:8080/test/a.html',
  scripts: [
    '.5.min.js'
  ],
  done: function(errors, window) {
    var $ = window.$;
    window.onload();
    console.log(window.a);
  }
});

and the html page here

<html>
<head>
    <script>
    var a = 0;
    function loads(){
        a=1000;
    }
    </script>
</head>
<body onload='loads()'>
</body>
</html>

and got error message below

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)

It reports the loads function was not defined, but it actually was declared in the web page.

Can anyone give some suggestions, or just simply tell me jsdom cannot process the scripts embedded in page.

I am new to nodejs. I want to use jsdom to parse some web pages which may contain script code inside. But I got error said the function or variable was not defined. Can anyone give some directions about this.

my code

var jsdom = require('jsdom');

jsdom.env({
  html: 'http://10.200.0.10:8080/test/a.html',
  scripts: [
    'http://code.jquery./jquery-1.5.min.js'
  ],
  done: function(errors, window) {
    var $ = window.$;
    window.onload();
    console.log(window.a);
  }
});

and the html page here

<html>
<head>
    <script>
    var a = 0;
    function loads(){
        a=1000;
    }
    </script>
</head>
<body onload='loads()'>
</body>
</html>

and got error message below

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)

It reports the loads function was not defined, but it actually was declared in the web page.

Can anyone give some suggestions, or just simply tell me jsdom cannot process the scripts embedded in page.

Share Improve this question edited May 12, 2012 at 14:17 mihai 38.6k11 gold badges62 silver badges89 bronze badges asked Apr 17, 2012 at 5:58 user966085user966085 3031 gold badge4 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Here's the code that you want to have:

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./jquery-1.5.min.js", function() {
    console.log(window.a);
    console.log(window.$().jquery); //jquery version
});

Your code will not work because the jsdom.env method does not process scripts.

Please note that you should use an awesome npm library request

Try this:

var http = require('http'),
    jsdom = require('jsdom'), 
    request = require('request');

var server = http.createServer(function (request, response) {
    getPage("http://isohunt./torrents/?iht=-1&ihq=life+is+beautiful", function (body) {
        jsdom.env({
            html: body,
            scripts: ['http://code.jquery./jquery-1.6.min.js']
        }, function(err, window){
            var $ = window.jQuery;
            //your code goes here

        });
    })
});
server.listen(3000);

function getPage(someUri, callback) {
    request({uri : someUri}, function (error, response, body) {
       callback(body);
    });
}

本文标签: javascriptjsdom doesn39t process script inside web pageStack Overflow