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

Opening a URL within PhantomJS


This recipe expands on the webpage object and introduces its open method. Here, we will focus on the basic version of open, which takes a URL and a simple callback function.

Getting ready

To run this recipe, we will need a script that accesses a web page. For this example to complete, an Internet connection is also required.

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

How to do it…

Consider the following script:

var webpage = require('webpage').create();

webpage.open('http://blog.founddrama.net/', function(status) {
  switch (status) {
    case 'success':
      console.log('webpage opened successfully');
      phantom.exit(0);
      break;
    case 'fail':
      console.error('webpage did not open successfully');
      phantom.exit(1);
      break;
    default:
      console.error('webpage opened...