What is onerror() Method in JavaScript? Last Updated : 18 Apr, 2024 Comments Improve Suggest changes Like Article Like Report The onerror() method in JavaScript is an event for error handling that occurs during the execution of code. It provides the mechanism to capture and handle runtime errors ensuring graceful error handling and debugging in the web applications. You can implement the onerror() method using the below methods: Table of Content Using Inline Event HandlerUsing addEventListener() methodUsing Inline Event HandlerIn this approach, we attach the onerror() event handler directly to the specific elements or objects in the HTML markup as an attribute. Syntax:<HTMLElement onerror="eventHandlerMethod()"> // Element Content</HTMLElement>Example: The below code will explain how you can attach onerror method using inline event handler. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title> The onerror() Method Example </title> </head> <body style="text-align: center;"> <h2> Click the below button to remove the src of image <br> to trigger the onerror() event method. </h2> <img onerror="handleError()" src= "https://media.geeksforgeeks.org/wp-content/uploads/20240401152503/javascript.jpg"> <br><br> <button onclick="removeSrc()"> Remove Image src </button> <script> const image = document.querySelector('img'); function removeSrc(){ image.setAttribute('src', ""); } function handleError() { alert('Error loading image!'); } </script> </body> </html> Output: Using addEventListener() methodThis approach involves attaching an event listener to the target element using addEventListener() method. Syntax:document.getElementById('image').addEventListener('error', handleError);Example: The below code explains the use of addEventListener() method to attach the onerror() event. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title> The onerror() Method Example </title> </head> <body style="text-align: center;"> <h2> Click the below button to remove the src of image <br> and trigger the onerror() event method. </h2> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20240401152503/javascript.jpg"> <br><br> <button> Remove Image src </button> <script> const image = document.querySelector('img'); const btn = document.querySelector('button'); image.addEventListener('error', handleError); btn.addEventListener('click', removeSrc); function removeSrc(){ image.setAttribute('src', ""); } function handleError() { alert('Error loading image!'); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article What is onerror() Method in JavaScript? M maha123 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads What is Math in JavaScript? JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions 2 min read What does +_ operator mean in JavaScript ? Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used t 2 min read JavaScript Object entries() Method The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.Syntax:Object.entries(obj);Parameters:obj 4 min read What is Undefined X 1 in JavaScript ? JavaScript is one of the most popular lightweight, interpreted compiled programming languages. It is single-threaded and synchronous in nature. Scripts(program in JavaScript) re executed as plain text. They can be either written directly on our page or in an external JavaScript file. JavaScript can 2 min read String blink() Method in Javascript In Javascript, the blink() method is a string method that is used to display a string inside the blink tag. Syntax: string.blink() Note: This method has been DEPRECATED and is no longer recommended. Parameter Values: This method does not accept any parameters. Return Values: It returns a string valu 1 min read JavaScript Object Methods Object Methods in JavaScript can be accessed by using functions. Functions in JavaScript are stored as property values. The objects can also be called without using brackets (). In a method, 'this' refers to the owner object.Additional information can also be added along with the object method.Synta 2 min read JavaScript Object defineProperties() Method The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object.Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are g 2 min read JavaScript Number valueOf( ) Method JavaScript Number valueOf( ) method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code Syntax: number.valueOf() Parameters: This method does not accept any parameter. Return Value: The valueof() metho 1 min read JavaScript Function.prototype.bind() Method A function is a set of statements that take inputs, do some specific computation, and produce output. There are various scenarios in programming in which we need to pre-configure this keyword or function arguments and we can easily do that in JavaScript with the help of the bind() method. The bind() 2 min read JavaScript Generator next() Method JavaScript Generator.prototype.next() method is an inbuilt method in JavaScript that is used to return an object with two properties done and value. Syntax: gen.next( value ); Parameters: This function accepts a single parameter as mentioned above and described below: value: This parameter holds the 2 min read Like