This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
API Reference phantom
James M. Greene edited this page May 23, 2013
·
11 revisions
This is a living document. As the codebase is updated, we hope to keep this document updated as well. Unless otherwise stated, this document currently applies to the latest PhantomJS release: PhantomJS 1.8.0
Note: This page serves as a reference. To learn step-by-step on how to use PhantomJS, please refer to the Quick Start guide.
## Object: `phantom` ## The interface with various PhantomJS functionalities is carried out using a new host object named `phantom`, added as a child of the [`window` object](https://developer.mozilla.org/en/DOM/window). The properties and functions of the `phantom` object are described in the following sections. ### Properties ### #### `args` {String[]} #### **Stability:** _DEPRECATED_ - Use [`system.args`](API-Reference-system#wiki-system-args) from the [System module](API-Reference-system) Read-only. An array of the arguments passed to the script. #### `cookies` {[Cookie](#cookie)[]} #### **Introduced:** PhantomJS 1.7 Get or set [Cookies](#cookie) for any domain (though, for setting, use of [`phantom.addCookie`](#phantom-addCookie) is preferred). These Cookies are stored in the CookieJar and will be supplied when opening pertinent WebPages. This array will be pre-populated by any existing Cookie data stored in the cookie file specified in the PhantomJS [startup config/command-line options](API-Reference#command-line-options), if any. #### `cookiesEnabled` {Boolean} #### **Introduced:** PhantomJS 1.7 Controls whether the CookieJar is enabled or not. Defaults to `true`. #### `libraryPath` {String} #### This property stores the path which is used by [`injectJs`](#phantom-injectJs) function to resolve the script name. Initially it is set to the location of the script invoked by PhantomJS. #### `scriptName` {String} #### **Stability:** _DEPRECATED_ - Use [`system.args[0]`](API-Reference-system#wiki-system-args) from the [System module](API-Reference-system) Read-only. The name of the invoked script file. #### `version` {Object} #### Read-only. The version of the executing PhantomJS instance. Example value: `{ 'major': 1, 'minor': 7, 'patch': 0 }`. ### Functions ### #### `addCookie([Cookie](#cookie))` {Boolean} #### **Introduced:** PhantomJS 1.7 Add a [Cookie](#cookie) to the CookieJar. Returns `true` if successfully added, otherwise `false`. See [`phantom.cookies`](#phantom-cookies) for more information on the CookieJar.Example:
phantom.addCookie({
'name': 'Added-Cookie-Name',
'value': 'Added-Cookie-Value',
'domain': '.google.com'
});
Example:
phantom.deleteCookie('Added-Cookie-Name');
Example:
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};