Listening to page events
Unlike a real browser, where we can see the page events happening visually, in PhantomJS, we cannot see anything. For example, on a real browser, we can see the status bar of the browser changing from one message to another when loading subpages and resources; we can also see that the images and text are slowly showing up as the entire page is being rendered by the browser. In PhantomJS, we can get information when these events and callbacks are handled appropriately.
There are two events that are related to page loading: onLoadStarted
and onLoadFinsihed
. You can attach callbacks to the page object for each event.
The onLoadStarted
event will be triggered when PhantomJS starts loading the page based on the resources received. The function takes no parameters. It is just a signal that the loading process has started.
page.onLoadStarted = function() { console.log("Page Loading Started"); };
When the page load is complete, another event will...