PhantomJS onPrompt 回调函数使用实例


发布日期 : 2024-09-27 07:08:00 UTC

访问量: 10 次浏览

PhantomJS onPrompt()

当网页调用prompt时,将调用此回调函数。它接受两个参数, messageanswer 。返回值是一个字符串。

语法

onPrompt() 语法如下所示 −

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