PhantomJS - injectJs()



injectJs 方法從指定檔案向頁面注入外部指令碼。如果檔案不在當前目錄中,則它將使用 libraryPath 進一步搜尋該檔案。如果檔案已注入,則返回 true,否則返回 false。

語法

其語法如下 −

wpage.injectJs(filename); 

示例

以下示例展示如何使用 injectJs() 方法。

var wpage = require('webpage').create(); 
wpage.open('https:///tasks/a.html', function(status) { 
   if (wpage.injectJs('tscript1.js')) { 
      var msg = wpage.evaluate(function() { 
         return testcode(); 
      }); 
      console.log(msg); 
      phantom.exit(); 
   } 
});

tscript1.js

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

以上程式生成以下輸出

welcome to phantomjs 
phantomjs_webpage_module_methods.htm
Advertisement
© . All rights reserved.