- 檔案系統模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 系統模組
- PhantomJS - 屬性
- Web 伺服器模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 雜項
- 命令列介面
- PhantomJS - 螢幕截圖
- PhantomJS - 頁面自動化
- PhantomJS - 網路監控
- PhantomJS - 測試
- PhantomJS - REPL
- PhantomJS - 示例
- PhantomJS 實用資源
- PhantomJS - 快速指南
- PhantomJS - 實用資源
- PhantomJS - 討論
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