新加载的javascript文件不能在phantomjs中工作


newly loaded javascript file is not working in phantomjs

我创建了一个js文件,其中有一些变量和常量。例如:

first.js 
var a = 10;
b = 15;

我需要将这个文件包含在我的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
}