sgentle/phantomjs-node — pull request #175 by Glavin001 (2014-06-03 03:59:23+00:00)
Title: Add page.onConsoleMessage instead of using set('onConsoleMessage', cb)

> Add `page.onConsoleMessage` instead of using `page.set('onConsoleMessage', function(){})`

Credit goes to @alexprobchansky for the solution.

Fixes #130, #125, #137.
Do note that this does **not** *fix* usage of `page.set('onConsoleMessage', function(){})` but instead provides an API that DOES work and passes an equivalent test (see test included).

## Before

```javascript
var phantom;
phantom = require('phantom');
phantom.create(function(phantomInstance) {
return phantomInstance.createPage(function(page) {
return page.open("file.html", function() {
// --- START, would crash here
page.set('onConsoleMessage', function(msg) { console.log(msg); });
// --- END
page.evaluate(function() {
return console.log("I will crash you fool!");
});
return phantomInstance.exit();
});
});
});
```

## After

```javascript
var phantom;
phantom = require('phantom');
phantom.create(function(phantomInstance) {
return phantomInstance.createPage(function(page) {
return page.open("file.html", function() {
// --- START, no longer crashes
page.onConsoleMessage(function(msg) { console.log(msg); });
// --- END
page.evaluate(function() {
return console.log("I will crash you fool!");
});
return phantomInstance.exit();
});
});
});
```