Debugging JavaScript in Netscape

Jake Howlett, 22 March 2001

Category: JavaScript; Keywords: debug alert Netscape onerror

JavaScript and Netscape. Now there are two words that, when heard in the same sentence, put the fear of god in me. I know that Netscape invented JavaScript but that doesn't distract from the fact that getting anything to work in it is a pain. The worst part has to be when it comes to debugging. The version I have installed on my PC here at work is 4.5. When I get an error it very kindly tells me so in the Status Bar.

Image

Why do we have to type "javascript:" in to the "Location"? Why can't it just tell us? Rather annoying when we are doing lots of debugging to have to type this in every time. Also, if you are as stupid as me, and you type "javascript" instead of "javascript:" then you are left totally clueless.

What we really want is something more like Internet Explorer where the reported error is a little easier to find. We can even take it one step further and actually debug the code, as discussed in this article. This is possible in Netscape using the Netscape JavaScript Debugger 1.1 . However, as with everything Netscape it is bugful. Getting it installed is one thing. Starting it and using it is a whole different ball-game.

Let's see if we can't make it a little simpler. Using the onerror event of the window object we can call a function that alerts the user (or developer if it's only being used in debug mode) what the error was and how it ocurred. First thing to do is write the function to handle the errors:

function errorHandler( e, f, l ){
alert("An error has ocurred in the JavaScript on this
page.\nFile: " + f + "\nLine: " + l + "\nError:" + e);
return true;
}
Returning true at the end of the function tells Netscape not to bother doing its own messages, whereas false lets it do its own stuff. All we need do now is assign this function to the window's onerror event:
if (navigator.appName == "Netscape") 
window.onerror = errorHandler;
Notice that we only assign this to the onerror event for Netscape, after all, Internet Explorer does a pretty good job already. So, what does this do? Well instead of reporting the error to the status bar we get a prompt like the one below:

Image

This popped up when I clicked a button that ran code that with a call to a variable called "trigger". Obviously here I mistyped it.

In summary this is still by no means a perfect solution, but is definitely a whole lot nearer that what we had been given. The adventurous amongst may well go and download the JavaScript debugger for Netscape. Good luck.

Those of us who have started using Version 6.0 may be relieved to have found the JavaScript error console. However this still isn't exactly the perfect solution. It still begs the question: Where EXACTLY is the error!!??!?!

Image

Maybe they will get it right in version 7.0. Or maybe 8.0, or 9.0 or......