PhantomJS - includeJS() 方法

includeJs方法在页面上执行外部JS文件,并在完成后执行回调函数。

语法

其语法如下 −

var wpage = require('webpage').create();
wpage.includeJs(jsfile,function(){});

示例

以下示例显示了includeJs()方法的使用。

var wpage = require('webpage').create();  
wpage.onConsoleMessage = function (str) { 
   console.log('CONSOLE: ' + msg); 
} 
wpage.open('http://localhost/tasks/a.html', function(status) { 
   wpage.includeJs('http://localhost/tasks/testscript.js', function() { 
      var foo = wpage.evaluate(function() { 
         return testcode(); 
      }); 
      console.log(foo); 
   }); 
}); 

testscript.js

function testcode () { 
   return "welcome to phantomjs"; 
}

上述程序生成以下输出

welcome to phantomjs 

phantomjs_webpage_module_methods.html