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.htm
广告