Click here to Skip to main content
11,647,170 members (70,183 online)
Click here to Skip to main content

Javascript Error Blocker

, 3 Oct 2007 GPL3 25.8K 14
Rate this:
Please Sign up or sign in to vote.
Javascript Error Blocker

Introduction

This is all about Error Stopping, do not mix it up with Error Blocking. But it does fantastic job in blocking any javascript related errors to come in your web page.

Background

The Story start as i was developing a website. It had extensive use of AJAX and asyncronous calls to web services. The main requirement was to make it cross browser compliant. To do this extensive javascript was used. Now change is a sure thing to come, so it happened. And on some forms, which were working fine, javascript errors start coming. Obviously they were not related to main program. They were coming due to change in website design, addition of fields on some forms. If i have been started to remove those, it could take a year to do so. Then i used the code below and it solved my problems. Now not a single error comes on any of my webpage, and functionality is also working out great.

Using the code

What we have to do is that just copy the code below in Head or Body section of our webpage. And leave the rest to it. Toggle track_errors to 0 or 1. 0 means Error Blocker is off and 1 means it is on. In the code snippet below i have turned Error blocking to on status.

<script language="javascript">
var track_errors=1;
function noError()
{
  if (track_errors==1)
     {
        return true;
     }
}
window.onerror = noError;
</script>

Option above is page level.If you want to make this option global then alternate way is to create a file named validate.js and copy the following code in it:

var track_errors=1;
function noError()
{
  if (track_errors==1)
     {
        return true;
     }
}
window.onerror = noError;

and reference it in your page as

<script src="validate.js" type="text/javascript"></script>

One thing to keep in mind is that use it after your development is done. As there may be crucial javascript functions that needs to be tested or included. If error comes in those then you will not be able to track those.

Points of Interest

A Great Error Blocker. Works for me everytime. Though a small piece of code. But if you like it, Please do vote for me on this.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

AliSufyan
Architect
Pakistan Pakistan
Ali is an MVP for Visual C#, he loves to program, write code, articles and blog on http://www.devride.com. His major interests are windows phone, Windows 8 metro programming using C# and game programming.

You may also be interested in...

Comments and Discussions

 
GeneralA better way [modified] Pin
Nick Taylor9-Oct-07 6:24
memberNick Taylor9-Oct-07 6:24 
Questionwhat about try / catch ? Pin
Brian Roberts9-Oct-07 0:07
memberBrian Roberts9-Oct-07 0:07 
GeneralError is still coming Pin
Jigar K Oza4-Oct-07 20:31
memberJigar K Oza4-Oct-07 20:31 
AnswerRe: Error is still coming Pin
yashmak9-Oct-07 19:21
memberyashmak9-Oct-07 19:21 
GeneralNOT a good idea!!! Pin
ghmcadams4-Oct-07 8:23
memberghmcadams4-Oct-07 8:23 
AnswerRe: NOT a good idea!!! Pin
Code Monkey8-Oct-07 11:12
memberCode Monkey8-Oct-07 11:12 
GeneralRe: NOT a good idea!!! Pin
SeanKinsey8-Oct-07 22:16
memberSeanKinsey8-Oct-07 22:16 
GeneralRe: NOT a good idea!!! Pin
AliSufyan18-Oct-07 3:36
memberAliSufyan18-Oct-07 3:36 
GeneralRe: NOT a good idea!!! Pin
thany.org15-Nov-07 21:45
memberthany.org15-Nov-07 21:45 
GeneralVery Nice Pin
merlin9814-Oct-07 3:25
membermerlin9814-Oct-07 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150804.2 | Last Updated 4 Oct 2007
Article Copyright 2007 by AliSufyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid