当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PhantomJS onNavigationRequested()用法及代码示例



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

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

  • Type− 类型的值为 undefined、Linkclicked、FormSubmitted、BackorForward、Reload、FormReSubmitted、Other。

  • willNavigate- 如果导航将发生,则为真,如果锁定则为假。

  • Main− 如果来自主窗口则为真,如果来自 iframe 或任何其他子框架则为假。

用法

它的语法如下 -

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();
   } 
});

上述程序生成以下内容output

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 - onNavigationRequested()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。