- PhantomJS - Home
- PhantomJS - Overview
- PhantomJS - Environment Setup
- PhantomJS - Object
- PhantomJS - Methods
- PhantomJS - Properties
- PhantomJS - Methods
- PhantomJS - Events/Callbacks
- PhantomJS - Child Process Module
- File System Module
- PhantomJS - Properties
- PhantomJS - Methods
- System Module
- PhantomJS - Properties
- Web Server Module
- PhantomJS - Properties
- PhantomJS - Methods
- Miscellaneous
- Command Line Interface
- PhantomJS - Screen Capture
- PhantomJS - Page Automation
- PhantomJS - Network Monitoring
- PhantomJS - Testing
- PhantomJS - REPL
- PhantomJS - Examples
- PhantomJS Useful Resources
- PhantomJS - Quick Guide
- PhantomJS - Useful Resources
- PhantomJS - Discussion
PhantomJS - onResourceRequested()
This is invoked when the page requests a resource. It has two arguments requestData and networkRequest.
The RequestData object has the following details −
Id − The number of the requested resource.
Method − The http method.
URL − The URL of the requested resource.
Time − The date object containing the date of the request.
Headers − The list of the http headers.
The NetworkRequest object has the following details −
Abort () − It aborts the current network request. Aborting the current network request will invoke the onResourceError callback function.
ChangeUrl (newurl) − The requested URL can be changed to a new file using this function.
SetHeader − It has the key and the value.
Syntax
Its syntax is as follows −
page.onResourceRequested = function(requestData, networkRequest) {}
Example
var wpage = require('webpage').create();
wpage.onResourceRequested = function(requestdata , networkdata) {
console.log("Data from requestdata:");
console.log(JSON.stringify(requestdata));
console.log("Data from networkdata");
console.log(JSON.stringify(networkdata));
}
wpage.open('http://localhost/tasks/request.html', function(status) {
});
The above program generates the following output.
Data from requestdata:
{"headers":[{"name":"Accept","value":"text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8"},{"name":"User-Agent","value":"Mozilla/5.0
(Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko)
PhantomJS/2.1.1 Safari/538.1"}], "id":1,"method":"GET","time":"2017-0507T13:25:36.454Z",
"url":"http://localhost/tasks/request.html"}
Data from networkdata
{"objectName":""}