PhantomJS - onPrompt()

当网页调用提示时,将调用此回调。它需要两个参数,messageanswer。返回值是一个字符串。

语法

其语法如下 −

wpage.onPrompt = function(msg, defaultVal) {}

示例

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

var wpage = require('webpage').create(); 
wpage.onPrompt = function(msg, answer) { 
   console.log("Entering in onPrompt callback"); 
   console.log(msg); 
   return answer; 
} 
wpage.open('http://localhost/tasks/prompt.html', function(status) { 
   console.log(status);  
}); 

prompt.html

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

上述程序生成以下输出

Entering in onPrompt callback
Is the page loaded
Success

phantomjs_webpage_module_events_callbacks.html