Browse DevX
Sign up for e-mail newsletters from DevX


Eliminate Irritating JavaScript Errors

No one likes those irritating JavaScript error messages, but to eliminate them you must add client-side error handling to your scripts, or, risk losing your audience.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

ou come home, turn on the computer, pull up a Web site, click on a link and HALT! A JavaScript alert box pops up; telling you there is a runtime error, and asking, "Do you wish to debug?" Well that message is fine for developers, but what about the end-users—your audience? The truth is the audience expects the Internet to be quick, easy and free. When they see errors, they mostly don't know what to do, so they take the path of least resistance—by closing the Alert box and clicking a different link or, worse, leaving your site altogether. To avoid this problem, you need to learn how to avoid the typical JavaScript error messages, discuss their common causes, and how to trap and handle them so you don't lose your audience.

JavaScript can raise object or syntax errors for several reasons: bad user input, bad data types, or simple typos in your code. By default, all JavaScript errors (depending on the browser version and the settings) produce an alert dialog asking users if they wish to debug. The dialog displays the line number and an error message, such as "Object expected" or "Character Expected" and (in some cases) shows additional information—such as the missing character. Netscape Navigator and Internet Explorer handle errors differently, so I'll show you ways to trap and handle errors in both browsers. Using the Throw, Try, and Catch Statements
Microsoft JScript version 5, which ships with Internet Explorer 5+, introduced the throw, try and catch statements. The try and catch statements give you the ability to handle runtime errors during their execution with little to no performance loss. JScript 5 also introduced the Error object. JScript throws instances of the Error object whenever an error occurs in your code. The try statement lets you invoke and "try" to execute a function or code block. When there are no errors the code executes; otherwise, you can catch the error object in a "catch" block, and handle the error in a more user-friendly manner.

Author Note: With the ECMAScript standards in version 2, you can use either the try/catch or the onerror event to trap and handle your errors. Netscape Navigator 6 and Internet Explorer 5+ support them both. Note that these methods will not work in browsers earlier than version 5; however, for IE-only clients you can use client-side VBScript which has an "On Error Resume Next" statement to prevent automatic error message displays.

The error object has two properties: number and description. The number property contains a two-byte integer. To get the "real" error number, you must mask this integer with 0xffff using the bit manipulation operator (the ampersand & character). The description property returns a string containing a description of the error.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date