當出現 JavaScript 錯誤時調用 OnError() 方法。 onError 方法的參數是msg和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>
上述程序生成以下內容output。
CONSOLE Message:ReferenceError:Can't find variable:page [{"file":"http://localhost/tasks/test.html","line":8,"function":"onload"}]
相關用法
- PhantomJS onConsoleMessage()用法及代碼示例
- PhantomJS onResourceRequested()用法及代碼示例
- PhantomJS onPageCreated()用法及代碼示例
- PhantomJS onUrlChanged()用法及代碼示例
- PhantomJS onNavigationRequested()用法及代碼示例
- PhantomJS onAlert()用法及代碼示例
- PhantomJS onResourceError()用法及代碼示例
- PhantomJS onResourceReceived()用法及代碼示例
- PhantomJS onFilePicker()用法及代碼示例
- PhantomJS onResourceTimeout()用法及代碼示例
- PhantomJS onCallback()用法及代碼示例
- PhantomJS onConfirm()用法及代碼示例
- PhantomJS onPrompt()用法及代碼示例
- PhantomJS open()用法及代碼示例
- PhantomJS setContent()用法及代碼示例
注:本文由純淨天空篩選整理自 PhantomJS - onError()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。