PhantomJS - onError()



The OnError() method is called when there is a JavaScript error. The arguments for the onError method are msg and stack trace, which is an array.

Syntax

Its syntax is as follows −

page.onError = function(msg, trace) {} 

Example

The following code shows the use of onError() method.

var wpage = require('webpage').create(); 
wpage.onError = function(msg, trace) {  
   console.log('CONSOLE Message: ' + msg ); 
   console.log(JSON.stringify(trace)); 
}; 
wpage.open('http://localhost/tasks/test.html', function(status) {   
   phantom.exit(); 
}); 

test.html

<html> 
   <head> 
      <title>Welcome to phantomjs</title> 
   </head> 
   
   <body> 
      <script type = "text/javascript"> 
         window.onload = function(){ 
            console.log(page); 
         } 
      </script> 
      
      <h1>This is a test page</h1> 
   </body>
   
</html> 

The above program generates the following output.

CONSOLE Message: ReferenceError: Can't find variable: page 
[{"file":"http://localhost/tasks/test.html","line":8,"function":"onload"}] 
phantomjs_webpage_module_events_callbacks.htm
Advertisements