Anticipating the page loading error

Since we will soon be dealing with page loading, we should have some capabilities to identify whether or not the page is properly loaded. We can do this by checking the status of the loading of the page using the webpage object's open callback.

var system = require('system'),
var url = system.args[1];
var page = require('webpage').create();
page.open(url, function(status) {
  if(status == 'success') {
    console.log('Page loaded.'),
    // do more stuff here on the loaded page
  } else {
    console.log('Ooops! Problem loading page: ' this.url);
    phantom.exit(1);
  }
});

The open method's second parameter is a callback that will be executed after the page loads, with or without an error. The function callback will have a single parameter that will hold the status of the page loading operation. The object is in the string format.

The parameter will have the value 'success' if the page is loaded, and 'fail' if there is a problem and the loading operation is not complete. In the preceding code, we have a reference to this.url, which is webpage.url; we should know that each and every property that the webpage object can be referenced as this within the function. There are several other properties that we can look into, such as frame information using frameTitle, frameUrl, framesCount, and so on. We can check out other webpage properties from the URL https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage.

In the preceding example, we check if status is equal to 'success' and, if it is, then we display the appropriate message; otherwise, we will display that we have encountered a loading problem as shown in the following screenshot:

Anticipating the page loading error
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.111.6