This sample shows how to install a custom window.onerror DHTML event handler in order to prevent the browser's default window.onerror handler from displaying befuddling error messages to the user. A window.onerror event fires whenever a client-side script error is not handled by VBScript's On Error statement, or ECMAScript 2.0's exception handling feature.
This sample accompanies the SiteBuilder Network article "Handling and Avoiding Web Page Errors #2: Runtime Errors".
A while ago I created an HTML page to make it easier for my Uncle Jim to view pictures taken with his new digital camera. While this could also be done by simply enabling Web View in Windows Explorer for the folder where he stores his photos, my Uncle Jim's computer is pathetically slow and memory-challenged. Plus, the documentation still sucks for customizing Web Folder views (although that will change soon). Anyway, I showed Uncle Jim how to put a link to his photo page in his "Quick Launch" toolbar, and he was a happy camper.
Unfortunately, Uncle Jim liked the thing so much he sent it to all his friends. So what's wrong with that? Well, for starters some of his friends didn't have the FileSystemObject installed on their computers. Plus, it is one thing to ask my Uncle Jim to ignore that pesky "Security Alert" dialog that pops up when he opens his photo page, but another thing altogether to tell his friends that too. I mean, I don't want one of his friends getting accustomed to always ignoring that dialog - that unfortunate habit could come back to bite them in the you know what.
The critical component that enabled me to build the photo viewer page is the FileSystemObject in the Scripting Run-Time Library. The FileSystemObject is a really cool ActiveX control that is included in the Microsoft Scripting Library (scrrun.dll). Normally using the FileSystemObject in a client-side page is not recommended since it exposes local file system access (bad pages could use that functionality to nuke your hard disk). However, this is a sample we are creating ourselves for use on our own computer (i.e., it is safe enough for our own use). Anyway, I use the control to enumerate the photo files that are in the folder containing my photo viewer page.
Web pages that access ActiveX controls must handle run-time script errors that can occur when a script attempts to access a control that was not successfully loaded by the browser. There are many reasons why the browser might not load an ActiveX control, hence there is more than one way to solve this problem.
In this particular case, I want to know when the FileSystemObject isn't loaded correctly so that I can tell Uncle Jim's friends why the photo viewer isn't working - it is almost always due to the scripting library not being installed on their computer. It can also occur because they declined the "Security Alert" dialog, or if they have "High Security" selected for the "Intranet Zone" (local content is considered to be in the "Intranet Zone"). I'm pretty certain there are no other reasons this page can fail, so I install my own window.onerror handler that will get called whenever there is a script error in the photo viewer page. My custom window.onerror handler displays a super friendly message that explains what went wrong when the script error message is: "Automation server cannot create object". Otherwise I put up a generic alert box that tells them to call me (sometimes maybe I'm too nice).
Click here to launch the viewer.