jsdom没有加载外部资源

时间:2011-03-19 23:21:52

标签: javascript node.js jsdom

我似乎无法让jsdom执行外部脚本,我似乎无法找到任何具体的例子。当我调用我的测试函数时,我没有收到任何错误,但没有任何反应。

这是我的代码:

    var window = jsdom.jsdom(body).createWindow();

    jsdom.jQueryify(window, './lib/jquery.js', function () {
      console.log(window.$("#a1").text());
      window.testing();
      console.log(window.$("#a2").text());
    });

这是html的加载:

<html>
    <head>
            <script type="text/javascript" src="stuff.js"></script>
    </head>
    <body>
    Hi there

    <div id="a1">
    </div>

    <div id="a2">
    </div>

 </body>
</html>

我的测试功能:

function testing() {
    var a2 = document.getElementById("a2");
    a2.innerHTML = 'Second div';
    console.log("executed");
 }

1 个答案:

答案 0 :(得分:3)

查看jsdom docs

在其余代码之前尝试此操作:

require('jsdom').defaultDocumentFeatures = {
  FetchExternalResources   : ['script'], 
  ProcessExternalResources : ['script'],
  MutationEvents           : '2.0',
  QuerySelector            : false
}

var window = jsdom.jsdom(body).createWindow();

jsdom.jQueryify(window, './lib/jquery.js', function () {
  console.log(window.$("#a1").text());
  window.testing();
  console.log(window.$("#a2").text());
});

/ fyi#node.js IRC欢迎您:http://bit.ly/nodeIRC

相关问题