访问量: 9 次浏览
onError()当发生JavaScript错误时,将调用 OnError() 方法。
OnError方法的参数是msg和stack trace,其中stack trace是一个数组。
其语法如下所示 −
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('http://localhost/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":"http://localhost/tasks/test.html","line":8,"function":"onload"}]