本文介绍了用PhantomJS包含js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PhantomJS脚本中,我正在尝试加载定义数组的本地JavaScript文件:

In a PhantomJS script, I am trying to load a local JavaScript file that defines an array:

var webPage = require('webpage'),
    page = webPage.create();

injected = page.injectJs('./codes.js');
if (injected) {
    console.log('injected codes.js');
    console.log(myCodes);
}
phantom.exit();

codes.js

myCodes = new Array();
myCodes[0] = { "stuff": "here" };
// more like this

我希望myCodes数组可用。
但我收到了

I'd expect the myCodes array to be available.Yet I receive

ReferenceError:找不到变量:myCodes

ReferenceError: Can't find variable: myCodes


推荐答案

找到答案。不得不用phantom.injectJs导入文件,而不是page.injectJs。

Found the answer. Had to import the file with phantom.injectJs, not page.injectJs.

filename = './codes.js';
injected = phantom.injectJs(filename);
if (injected) {
    console.log('injected codes.js');
    console.log('myCodes data:', myCodes);
}

这篇关于用PhantomJS包含js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:42