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:///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:///tasks/test.html","line":8,"function":"onload"}] 
phantomjs_webpage_module_events_callbacks.htm
廣告
© . All rights reserved.