Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Loading custom modules in PhantomJS


In this recipe, we will learn how to load custom modules in our PhantomJS scripts using the require function. PhantomJS has several built-in modules, but we can also write our own (see the Creating a custom module for PhantomJS recipe earlier in this chapter) and import them in this way.

Getting ready

To run this recipe, we will need a custom module that we want to import using the require function; we can use the arg-parser.js module that we wrote in the Creating a custom module for PhantomJS recipe earlier in this chapter.

The script in this recipe is available in the downloadable code repository as recipe11.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Assuming that we are using our arg-parser.js script from the previous recipe, consider the following script:

var argParser = require('../lib/arg-parser'),
    args      = require('system').args.slice(1);

args = argParser...