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.
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 ){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:
alert("An error has ocurred in the JavaScript on this
page.\nFile: " + f + "\nLine: " + l + "\nError:" + e);
return true;
}
if (navigator.appName == "Netscape")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:
window.onerror = errorHandler;
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net