Exiting with error codes

In most cases, if we've encountered an error, we normally stop further execution of the script. It is logical to do that in most scenarios. For example, if we have a problem loading the required URL or file, we halt the process since we reference DOM objects within the page after loading; we cannot do so if the page is not available.

The PhantomJS exit function can accept a numeric value that will be used as the error code; it is used by calling the shell script.

else {
  console.log('Ooops! Problem loading page.');
  phantom.exit(1);
}

In the preceding code, we call the phantom.exit method and pass a value of 1; this will denote our exit code that, within our script, means the page is not loaded. We can return different exit ...

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.