PhantomJS - onError()



当出现JavaScript错误时,将调用OnError()方法。onError方法的参数是msg堆栈跟踪,它是一个数组。

语法

其语法如下−

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('https://127.0.0.1/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":"https://127.0.0.1/tasks/test.html","line":8,"function":"onload"}] 
phantomjs_webpage_module_events_callbacks.htm
广告