不取脚本这是我如何构建它:当我用HTTP替换HTML jQuery的路径jsdom本地文件系统
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<script src="./js/lib/vendor/jquery.js"></script>
<!-- <script src="http://code.jquery.com/jquery.js"></script> -->
<script type="text/javascript">
var a = $("body"); // script crashes here
var b = "b";
</script>
</body>
</html>
:
var fs = require("fs");
var jsdom = require("jsdom");
var htmlSource = fs.readFileSync("./test.html", "utf8");
var doc = jsdom.jsdom(htmlSource, {
features: {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0'
},
parsingMode: "auto",
created: function (error, window) {
console.log(window.b); // always undefined
}
});
jsdom.jQueryify(doc.defaultView, 'https://code.jquery.com/jquery-2.1.3.min.js', function() {
console.log(doc.defaultView.b); // undefined with local jquery in html
});
的HTML它的工作原理。本地路径完全相对于shell /实际节点脚本的工作目录。说实话,我甚至不知道为什么我需要jQueryify,但没有它的窗口从来没有jQuery,即使有它,它仍然需要HTML文档内的http源。
谢谢,但不幸的是我测试过,不是这样。即使我使用绝对http路径,我也无法让jquery在没有jQueryify的情况下工作。我也意识到创建的回调是错误的,我必须在jsdom调用之后使用doc.defaultView.b。 但脚本似乎总是崩溃。即使我不调用$,没有jQueryify的任何外部脚本(!)使得b未定义(通过virtualConsole没有错误)。 – brannigan
实际上,如果您提供了juqery包的完整路径,该程序将起作用。我认为这是路径查找器中的问题。 –