PhantomJS - open()



The open method is used to open up a webpage. The open method takes a page URL and has a callback function, which is called when a page is loaded. The callback function is optional and can be used when required. The callback function contains the status, which defines the success or the failure for the page.

Syntax

Its syntax is as follows −

var wpage = require('webpage').create(); 
wpage.open(url, function(status) { 
   //status is success or failure 
}); 

open() with GET method

var wpage = require('webpage').create();  
wpage.open('http://www.google.com/', function(status) { 
   console.log(status); 
   phantom.exit(); 
}); 

The above program generates the following output.

Success

open() with POST method

var wpage = require('webpage').create(); 
var postdata = "username = roy"; 
wpage.open('http://localhost/tasks/a.php', 'POST',postdata, function(status) { 
   console.log(status); 
   console.log(wpage.content); 
   phantom.exit(); 
});

a.php

<?php 
   print_r($_POST); 
?> 

The above program generates the following output.

success 
<html><head></head><body>Array 
( 
   [username] => roy 
) 
</body></html>
phantomjs_webpage_module_methods.htm
Advertisements