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

Top Questions Asked About Invoice Financing Applications

Are you an owner of a small business who’s trying to come up with ways…

1 day ago

Complete Medical Guide to Blood Sugar Testing and Glucose Control

Introduction When patients bring me their lab reports, the confusion is almost always the same.…

4 days ago

6 Tips to Strategically Remodel Your Home

Are you excited about remodeling your house after a long time? Perhaps if you're planning…

5 days ago

Driving Finding Balance on and off the Mat: How Calm Awareness Supports Everyday

The practice of yoga teaches us to be present, patient and mindful of our decisions.…

6 days ago

7 Things to carry with you on hill station

As we are once again allowed to go outside, nearly everyone is planning to go…

1 week ago

Professional Cleaner Hire Guide for Your Business

How do you find a cleaning company that will take excellent care of your business…

1 week ago