PhantomJS - onNavigationRequested()



This callback tells when the navigation event is taking place. It takes the following four arguments −

  • URL − The target URL of the navigation event.

  • Type − Values for type are undefined, Linkclicked, FormSubmitted, BackorForward, Reload, FormReSubmitted, Other.

  • willNavigate − It is true, if navigation will take place, false if it is locked.

  • Main − It is true, if it comes from main window, false if it comes from iframe or any other sub frame.

Syntax

Its syntax is as follows −

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

Example

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

The above program generates the following 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

We are invoking the navigate callback on the reload of the page.

phantomjs_webpage_module_events_callbacks.htm
Advertisements