PhantomJS - onNavigationRequested()



此回撥在導航事件發生時進行提示。它採用以下四個引數:

  • **URL** - 導航事件的目標 URL。

  • **型別** - 該型別的取值為未定義、已點選連結、已提交表單、後退或前進、重新載入、已重新提交表單、其他。

  • **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('https:///tasks/wopen2.html', function(status) {
   console.log(status);
   
   if (status == success) {
      console.log(wpage.content);
      wpage.reload();
   } 
});

以上程式會生成以下 **輸出**。

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

我們正在頁面重新載入時呼叫導航回撥。

phantomjs_webpage_module_events_callbacks.htm
廣告
© . All rights reserved.