當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。