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('https:///tasks/a.html', function(status) { 
   wpage.includeJs('https:///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
廣告
© . All rights reserved.