下面的代码通过try…catch语句捕捉错误后弹出confirm对话框,如果用户点击cancel按钮则跳转至首页
<!DOCTYPE html> <html> <head> <script> var txt=""; function message() { try { adddlert("Welcome guest!"); } catch(err) { txt="There was an error on this page.\n\n"; txt+="Click OK to continue viewing this page,\n"; txt+="or Cancel to return to the home page.\n\n"; if(!confirm(txt)) { document.location.href="http://www.75271.com/"; } } } </script> </head> <body> <input type="button" value="View message" onclick="message()" /> </body> </html>