PhantomJS - onAlert()

当页面上出现警告时,将调用此回调。回调采用字符串,不返回任何内容。

语法

其语法如下 −

var wpage = require('webpage').create();
wpage.onAlert = function(msg) {};

示例

以下示例显示了 onAlert() 方法的使用。

var wpage = require('webpage').create(); 
wpage.onAlert = function(str) { 
   console.log(str); 
} 
wpage.open('http://localhost/tasks/alert.html', function(status) { 
   //wpage.stop(); 
   console.log(status);  
   
   phantom.exit(); 
});

alert.html

<html> 
   <head></head> 
   <body> 
      <script type = "text/javascript"> 
         alert("Welcome to phantomjs"); 
      </script> 
      <h1>This is a test page</h1> 
   </body> 
</html>

上述程序生成以下输出

Welcome to phantomjs 
Success 

phantomjs_webpage_module_events_callbacks.html