Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
新加载的javascript文件在phantomjs中不起作用_Javascript_Php_Phantomjs - Fatal编程技术网

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

新加载的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

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

我需要在我的
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(){
then
console.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
}