PhantomJS - onError() 方法

当 JavaScript 发生错误时,会调用 OnError() 方法。onError 方法的参数为 msgstack trace,后者是一个数组。

语法

其语法如下 −

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

示例

以下代码显示了 onError() 方法的使用。

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> 

上述程序生成以下输出

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

phantomjs_webpage_module_events_callbacks.html