DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: Javascript

JavaScript onerror() method to handle error

Share

The JavaScript onerror method is the first thing to handle error in JavaScript. This error event is called on the window object whenever any exception occurs on the page.

There are three pieces of information to identify the exact error:

  • Error message
  • URL, in which file the error occurred
  • The line number in the given URL where error occurred

Example of JavaScript onerror:

<html>
<head>
<script type="text/javascript">
window.onerror = function (msg, url, line) {
console.log("Message : " + msg );
console.log("url : " + url );
console.log("Line number : " + line );
}
</script>
</head>
<body>
<p>Click the following button:</p>
<form>
<input type="button" value="Click Here" >

Read Also: JavaScript Strict mode

Alternatively, you can use an onerror method to display an error message if there is any problem in loading an image.

<img src="image.jpg" onerror="alert('Loading failed')" />
Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Guided Help That Trains You for University and Beyond

In today's competitive academic world, students are constantly juggling multiple responsibilities, from preparing for exams…

22 hours ago

Discussing How Handbags Enhance Women’s Everyday Style

There is more to handbags than being accessories; they add to a look and tell…

2 days ago

Demolition Services for Homes and Commercial Premises

Every constructed premise has a certain survival time. A constructed house or commercial property has…

3 days ago

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

6 days ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

7 days ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

1 week ago