新加载的javascript文件在phantomjs中不起作用

时间:2014-03-12 13:13:56

标签: javascript php 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
}

如何在评估函数之外使用我的变量。

我非常简短地解释你。请尝试了解我的问题,并帮我解决。

1 个答案:

答案 0 :(得分:0)

你试过吗

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
}