injectJs 方法將來自指定文件的外部腳本包含到頁麵中。如果文件在當前目錄中不可用,它會使用 libraryPath 對文件進行額外搜索。如果文件被注入,它返回真,否則返回假。
用法
其語法如下:
wpage.injectJs(filename);
示例
以下示例顯示如何使用 injectJs() 方法。
var wpage = require('webpage').create();
wpage.open('http://localhost/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";
}
上述程序生成以下內容output。
welcome to phantomjs
相關用法
- PhantomJS includeJS()用法及代碼示例
- PhantomJS onConsoleMessage()用法及代碼示例
- PhantomJS setContent()用法及代碼示例
- PhantomJS switchToFocusedFrame()用法及代碼示例
- PhantomJS onResourceRequested()用法及代碼示例
- PhantomJS onPageCreated()用法及代碼示例
- PhantomJS deleteCookie()用法及代碼示例
- PhantomJS renderBase64()用法及代碼示例
- PhantomJS onUrlChanged()用法及代碼示例
- PhantomJS onNavigationRequested()用法及代碼示例
- PhantomJS onAlert()用法及代碼示例
- PhantomJS onResourceError()用法及代碼示例
- PhantomJS evaluateJavascript()用法及代碼示例
注:本文由純淨天空篩選整理自 PhantomJS - injectJs()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。