Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีเขียน Global Error Handler ใน JavaScript

Global Error Handler ต่อไปนี้แสดงวิธีจับ Unhandled Exception

ตัวอย่าง

<!DOCTYPE html> <html> 
  <body> 
    <script> 
      window.onerror = function(errMsg, url, line, column, error) { 
        var result = !column ? '' : '\ncolumn: ' + column; 
        result += !error; 
        document.write("Error= " + errMsg + "\nurl= " + url + "\nline= " + line + result); 
        var suppressErrorAlert = true; 
        return suppressErrorAlert; 
      }; 
      setTimeout(function() { 
        eval("{"); 
      }, 500) 
    </script> 
  </body> 
</html>