PhantomJS 页面导航监听:onNavigationRequested 用法与参数说明


发布日期 : 2021-08-29 21:48:06 UTC

访问量: 10 次浏览

PhantomJS onNavigationRequested()

此回调函数在导航事件发生时调用。它接受以下四个参数−

  • URL- 导航事件的目标 URL。
  • Type- type 的取值有 undefined、Linkclicked、FormSubmitted、BackorForward、Reload、FormReSubmitted、Other。
  • willNavigate- 如果导航将要发生,将为 true;如果被锁定,将为 false。
  • Main- 如果来自主窗口,则为 true;如果来自 iframe 或任何其他子框架,则为 false。

语法

其语法如下−

wpage.onNavigationRequested = function(url, type, willNavigate, main) {}

示例

var wpage = require('webpage').create();
wpage.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
console.log('Caused by: ' + type);
console.log('Will actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
}
wpage.open('http://localhost/tasks/wopen2.html', function(status) {
console.log(status);

if (status == success) {
console.log(wpage.content);
wpage.reload();
}
});

上面的程序会生成以下输出

Trying to navigate to: http://localhost/tasks/wopen2.html
Caused by: Other
Will actually navigate: true
Sent from the page's main frame: true
Success

在页面重新加载时,我们调用了导航回调函数。