PhantomJS - onCallback()
onCallback 方法用于将值从网页传递到网页对象,它是使用 window.callPhantom() 方法完成的。此方法在内部调用 onCallback 函数。
语法
其语法如下 −
var wpage = require('webpage').create(); wpage.onCallback = function(data) {}
示例
以下示例显示了 onCallback() 方法的使用。
var wpage = require('webpage').create(); wpage.onCallback = function(str) { console.log(str + " phantomJs"); } wpage.open('http://localhost/tasks/callback.html', function(status) { console.log(status); phantom.exit(); });
callback.html
<html> <head></head> <body> <script type = "text/javascript"> var a = window.callPhantom("Welcome to "); </script> </body> </html>
上述程序生成以下输出。
Welcome to phantomJs Success
phantomjs_webpage_module_events_callbacks.html