ÍøÂçѧԺ

w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS onerror

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

JavaScript
JSÊýѧ¶ÔÏó²Î¿¼
JS×Ö·û´®¶ÔÏó²Î¿¼
JSº¯Êý²Î¿¼
JSʼþ²Î¿¼
Javascript ³£ÓÃÕ..
FFºÍIEϵÄjs¼æÈÝ..
jQuery ¼òµ¥½éÉÜ
jQuery / ºËÐÄ / ..
jQuery / ºËÐÄ / ..
ÈçºÎʹÓÃJSÀ´ÅжÏ..
JavascriptÔÚIEºÍ..
3¸öjs×Ö·û±àÂ뺯Ê..
javascript ÖÐµÄ ..

JS onerror


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:12036 :: ::

Using the onerror event is the old standard solution to catch errors in a web page.
ʹÓÃonerrorʼþÊDz¶×½webÒ³´íÎóµÄ±È½ÏÀϵıê×¼·½·¨¡£


Examples
Àý×Ó

The onerror event[onerrorʼþ]
How to use the onerror event to catch errors in a web page.
ʹÓõķ½·¨ÑÝʾ


The onerror Event

We have just explained how to use the try...catch statement to catch errors in a web page. Now we are going to explain how to use the onerror event for the same purpose.
ÎÒÃÇÒѾ­½âÊÍÔõÑùʹÓÃtry...catchÉùÃ÷À´»ñÈ¡webÒ³µÄ´íÎó¡£ÏÖÔÚÎÒÃǽ«½Ó׎éÉÜÔõÑùÓÃonerrorÀ´´ïµ½ÏàͬµÄÄ¿µÄ¡£

The onerror event is fired whenever there is a script error in the page.
²»ÂÛʲôʱºòÖ»Òª½Å±¾³öÏÖ´íÎóonerrorʼþ¾Í»á±»¼¤»î

To use the onerror event, you must create a function to handle the errors. Then you call the function with the onerror event handler. The event handler is called with three arguments: msg (error message), url (the url of the page that caused the error) and line (the line where the error occurred).
ҪʹÓÃonerrorʼþ£¬Äã±ØÐ뽨Á¢Ò»¸öº¯ÊýÀ´´¦Àí´íÎó¡£msg(´íÎóÐÅÏ¢),url(³ö´íÒ³µÄurl)ºÍline(·¢Éú´íÎóµÄλÖÃ)

SyntaxÓï·¨

onerror=handleErr
function handleErr(msg,url,l)
{
//Handle the error here
return true or false
}

The value returned by onerror determines whether the browser displays a standard error message. If you return false, the browser displays the standard error message in the JavaScript console. If you return true, the browser does not display the standard error message.
·µ»ØÖµ¾ö¶¨ÊÇ·ñÔÚä¯ÀÀÆ÷ÉÏÏÔʾ±ê×¼µÄ´íÎóÐÅÏ¢¡£Èç¹ûÄã·µ»Øfalse£¬ä¯ÀÀÆ÷ÏÔʾÔÚJS¿ØÖÆÌ¨ÖеĴíÎóÐÅÏ¢¡£Èç¹ûÄã·µ»Øtrue£¬ä¯ÀÀÆ÷²»ÏÔʾ´íÎóÐÅÏ¢¡£

Example¾ÙÀý

The following example shows how to catch the error with the onerror event:
ÏÂÃæµÄÀý×ÓÑÝʾÔõÑùÓÃonerrorʼþÀ´²¶×½´íÎó£º

<html>
<head>
<script type="text/javascript">

onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt="There was an error on this page.nn"
txt+="Error: " + msg + "n"
txt+="URL: " + url + "n"

txt+="Line: " + l + "nn"
txt+="Click OK to continue.nn"
alert(txt)
return true
}
function message()
{
adddlert("Welcome guest!")
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>

ÆÀÂÛ (1) 1 All