弹出提示框的 try…catch 语句

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. var txt = "";
  6. function message() {
  7. try {
  8. adddlert("Welcome guest!");
  9. }
  10. catch(err) {
  11. txt = "此页面出错。\n\n";
  12. txt += "单击“确定”继续查看此页面,\n";
  13. txt += "或取消以返回主页。\n\n";
  14. if(!confirm(txt)) {
  15. document.location.href = "http://www.yousite.com/";
  16. }
  17. }
  18. }
  19. </script>
  20. </head>
  21.  
  22. <body>
  23. <input type="button" value="查看消息" onclick="message()" />
  24. </body>
  25.  
  26. </html>