|
【目录】 【上一页】 【下一页】 【索引】
onError
Executes JavaScript code when an error event occurs; that is, when the loading of a document or image causes an error.
语法
onError="handlerText"
参数
描述
An error event occurs only when a JavaScript语法 or runtime error occurs, not when a browser error occurs. For example, if you try set window.location.href='notThere.html' and notThere.html does not exist, the resulting error message is a browser error message; therefore, onError would not intercept that message. However, an error event is triggered by a bad URL within an IMG tag or by corrupted image data.
window.onerror applies only to errors that occur in the window containing window.onerror, not in other windows.
onError can be any of the following:
If you write an error-handling function, you have three options for reporting errors:
使用的事件属性
示例
示例 1: Null event handler. In the following IMG tag, the code onError="null" suppresses error messages if errors occur when the image loads.
<IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" border="2px" onError="null">
示例 2: Null事件适用对象a window. The onError事件适用对象windows cannot be expressed in HTML. Therefore, you must spell it all lowercase and set it in a SCRIPT tag. The following code assigns null to the onError handler for the entire window, not just the Image object. This suppresses all JavaScript error messages, including those for the Image object.
<SCRIPT> window.onerror=null </SCRIPT> <IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" border="2px">
However, if the Image object has a custom onError event handler, the handler would execute if the image had an error. This is because window.onerror=null suppresses JavaScript error messages, not onError event handlers.
<SCRIPT> window.onerror=null function myErrorFunc() { alert("The image had a nasty error.") } </SCRIPT> <IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" border="2px" onError="myErrorFunc()">
In the following example, window.onerror=null suppresses all error reporting. Without onerror=null, the code would cause a stack overflow error because of infinite recursion.
<SCRIPT> window.onerror = null; function testErrorFunction() { testErrorFunction(); } </SCRIPT> <BODY onload="testErrorFunction()"> test message </BODY>
示例 3: Error handling function. The following example defines a function, myOnError, that intercepts JavaScript errors. The function uses three arrays to store the message, URL, and line number for each error. When the user clicks the Display Error Report button, the displayErrors function opens a window and creates an error report in that window. Note that the function returns true to suppress the standard JavaScript error dialog.
<SCRIPT> window.onerror = myOnError
msgArray = new Array() urlArray = new Array() lnoArray = new Array()
function myOnError(msg, url, lno) { msgArray[msgArray.length] = msg urlArray[urlArray.length] = url lnoArray[lnoArray.length] = lno return true }
function displayErrors() { win2=window.open('','window2','scrollbars=yes') win2.document.writeln('<B>Error Report</B><P>')
for (var i=0; i < msgArray.length; i++) { win2.document.writeln('<B>Error in file:</B> ' + urlArray[i] + '<BR>') win2.document.writeln('<B>Line number:</B> ' + lnoArray[i] + '<BR>') win2.document.writeln('<B>Message:</B> ' + msgArray[i] + '<P>') } win2.document.close() } </SCRIPT>
<BODY onload="noSuchFunction()"> <FORM> <BR><INPUT TYPE="button" VALUE="This button has a语法 error" onClick="alert('unterminated string)">
<P><INPUT TYPE="button" VALUE="Display Error Report" onClick="displayErrors()"> </FORM>
This example produces the following output:
Error Report
Error in file: file:///c%7C/temp/onerror.html Line number: 34 Message: unterminated string literal
Error in file: file:///c%7C/temp/onerror.html Line number: 34 Message: missing ) after argument list
Error in file: file:///c%7C/temp/onerror.html Line number: 30 Message: noSuchFunction is not defined
示例 4: Event handler calls a function. In the following IMG tag, onError calls the function badImage if errors occur when the image loads.
<SCRIPT> function badImage(theImage) { alert('Error: ' + theImage.name + ' did not load properly.') } </SCRIPT> <FORM> <IMG NAME="imageBad2" SRC="orca.gif" ALIGN="left" border="2px" onError="badImage(this)"> </FORM>
参看
onAbort, onLoad
要获得关于事件句柄的常规信息,请看“事件的常规信息”。
要获得关于事件对象的信息,请看事件。
【目录】 【上一页】 【下一页】 【索引】
|