Chapter 9. External JavaScript

In most development projects, it is essential to organize our code into separate files or modules to facilitate readability and reuse. PhantomJS supports a module-loading mechanism that allows us to load and use external JavaScript. It uses the CommonJS API standard (http://wiki.commonjs.org).

Modules

In earlier chapters, we discussed PhantomJS's built-in modules. Let us explore how to create our own modules and use them in our PhantomJS scripts.

First, let's create a straightforward module of a timer that can measure the duration of a given process based on calling the start and stop functions. Let us define the variables to be used. We will require timeStart, timeStop, and a duration variable to hold the duration ...

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.