新加载的javascript文件在phantomjs中不起作用
我创建了一个js文件,其中包含一些变量和常量。 例如: 我需要在我的新加载的javascript文件在phantomjs中不起作用,javascript,php,phantomjs,Javascript,Php,Phantomjs,我创建了一个js文件,其中包含一些变量和常量。 例如: 我需要在我的phantomjs文件中包含这个文件并使用这些变量 我的幻影js代码是这样的 page.onLoadFinished = function() { setTimeout(function(){ var my_var = run_stats(); page.close(); system.stdout.writeLine(JSON.str
phantomjs
文件中包含这个文件并使用这些变量
我的幻影js代码是这样的
page.onLoadFinished = function() {
setTimeout(function(){
var my_var = run_stats();
page.close();
system.stdout.writeLine(JSON.stringify(my_var));
phantom.exit(0);
},2000);
};
page.open(phantom.args[0],function(status){
system.stderr.writeLine(status);
if(status !== 'success'){
console.log('unable to open the page '+phantom.args[0]);
phantom.exit(0);
}
});
function run_stats(){
page.injectJs('jquery-1.9.1.min.js');
page.injectJs('first.js');
my_var = page.evaluate(function() {
console.log(a); //here the variables are working
console.log(a);
}
console.log(a); //here, not working
}
在evaluate函数之外使用变量应该怎么做
我很简单地向你解释。请试着理解我的问题并帮助我解决它。你试过了吗
var aa ;
function run_stats(){
page.injectJs('jquery-1.9.1.min.js');
page.injectJs('first.js');
my_var = page.evaluate(function() {
console.log(a); //here the variables are working
console.log(a);
aa = a;
}
console.log(aa); //here, not working
}
您正在使用
page
调用'first.js'
,因此它在page.evaluate中工作,因为first.js
可以被page
读取,请尝试定义var aa代码>在my_var=page.evaluate
上方,然后在page.evaluate
内设置值aa=a
,在page.evaluate下方,添加console.log(aa)
这应该可以工作TypeError:“undefined”不是我console.log(aa)
时得到的对象;我把我的答案贴出来让你看得更清楚,你试过了吗?你的答案很清楚。。但是,对于值aa
wired,我得到了undefined
,请尝试定义一个to页面,例如page.a=a;在my_var=page.evaluate(function(){
thenconsole.log(page.a);
这是一个技巧
var aa ;
function run_stats(){
page.injectJs('jquery-1.9.1.min.js');
page.injectJs('first.js');
my_var = page.evaluate(function() {
console.log(a); //here the variables are working
console.log(a);
aa = a;
}
console.log(aa); //here, not working
}