PhantomJS - onNavigationRequested()

此回调告知导航事件何时发生。它需要以下四个参数 −

  • URL − 导航事件的目标 URL。

  • 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

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

phantomjs_webpage_module_events_callbacks.html