Error Reporting - Java Script

Because each browser has its own built-in JavaScript interpreter(including its own mechanisms for tracking down errors while interpreting),each reports errors in different ways: Some pop up an error message; others simply log the message to a JavaScript console.Regardless of the method used,be aware of where this data is located on your target browsers.Browsers such as Internet Explorer, Mozilla,Safari,and Opera each have their own unique ways of presenting this data.This section guides you to the error message information for each of them.

Internet Explorer (Windows)

Microsoft’s flagship browser,Internet Explorer for Windows,is capable of reporting errors in a couple of different ways.By default,Internet Explorer pops up a dialog with the error details and asks if you want to continue running scripts on the page. This is misleading, because clicking either Yes or No allows other scripts to continue running.

Internet Explorer (Windows)

If the browser has a debugger (such as Microsoft Script Debugge),the dialog offers the option to debug the script or ignore it .Clicking Yes on this dialog brings you into the debugger; clicking No simply closes the dialog.

Internet Explorer (Windows)

The other option is to notpop up an error dialog. This setting can be enabled by going to Internet Options,clicking on the Advanced tab, and unchecking “Display a notification about every script error”.

Internet Options

This setting causes Internet Explorer to display a small yellow icon with an X over it in the lower-left corner of the browser window whenever an error occurs.Double-clicking the icon brings up a dialog displaying the typical error information (message, URL, and line number).This dialog also enables you(by using the Previous button) to see any errors that occurred before the one being displayed.

errors that occurred before the one being displayed.

Internet Explorer (MacOS)

By default, Internet Explorer on the Macintosh doesn’t show JavaScript errors. To turn on this feature, go to the Preferences dialog(available under Edit ➪ Preferences).Under the Web Browser section on the left side of the window, click Web Content. In the lower part of the window, you see a check box labeled Enable Scripting; immediately underneath is a check box labeled Show Scripting Error Alerts.Checking this box causes the browser to pop up an error message when a JavaScript error occurs.

The problem with IE/Mac’s error alerts is that it doesn’t give an accurate line number for external scripts (the line number is typically the line in the HTML file where the script has been referenced).In extreme cases,it may be more useful to insert the script inline for testing purposes.

Mozilla (all platforms)

Mozilla features a JavaScript console that logs not only errors, but warnings as well.To access the JavaScript console,look under Tools ➪ Web Development ➪ JavaScript Console in Mozilla 1.0+ (prior to 1.0, the JavaScript console was located under Tasks ➪ Tools ➪ JavaScript Console).

Just like its predecessor Netscape Navigator, Mozilla allows you to type javascript: into the address box to open the JavaScript console instead of accessing it from the menu.

The JavaScript console reports three types of messages: errors, strict warnings, and messages.Errors are either syntax or runtime errors (anything that stops script execution), and they are reported with the error message, the filename, and the line number on which the error occurred .Strict warnings occur when the code does something illegal, but the interpreter works around it allowing the script to continue running (such problems include redefining a previously defined variable).Messages are purely informational and often are a result of internal Mozilla processing, not JavaScript.

Mozilla (all platforms)

Safari (MacOS X)

Macintosh’s Safari browser has perhaps the worst support for JavaScript errors and debugging. By default, it offers no JavaScript error reporting to the end user.In order to enable error reporting, follow these steps:

  1. Open a command line shell.
  2. Execute the following: defaults write com.apple.Safari IncludeDebugMenu 1.
  3. Restart Safari.
  4. Under the Debug menu, check Log JavaScript Exceptions (Figure 14-6).
  5. Start Console.app under Application/Utilities.

Safari (MacOS X)

After completing these steps,JavaScript errors are logged to the Console.app window.Unfortunately, the messages are less than useful without a URL or line number to help locate the cause of any given error. However,it is the best Safari can offer at this point.

Safari (MacOS X)

Opera 7(all platforms)

Similar to Mozilla, Opera 7 features a JavaScript console to aid in debugging.The console is accessible under Window ➪ Special ➪ JavaScript Console.The console provides the most data out of available browsers including the error type,error code, error message, the thread that the error occurred in, and a stack trace (labeled Backtrace in the window) indicating the origins of the error.

Opera 7(all platforms)

It is possible to have the JavaScript console come up whenever there is an error by going into Tools ➪ Preferences, and then clicking on Multimedia. You’ll see a check box titled Enabled JavaScript, along with a button labeled JavaScript Options..... If you click the button, a new dialog pops up, one of the options is Open JavaScript Console on Error. When this check box is checked (it is unchecked by default),the JavaScript console pops up whenever there is an error.

javascript options

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics