How do you supress javascript errors?

Posted on 2009-12-22
Last Modified: 2012-05-08
Can javascript errors be supressed or told not show like in php? In php I can use error_reporting(0) to prevent them from being shown to users. Can that also be done in javascript?

The reason is a lot of the sites that I work on, the errors come from an outside script that you don't have access to, but also the errors are not causing any issues. But in browsers I can see the little error icons lighting up. I would like to prevent that once I've confirmed that the errors aren't causing any problems or going to slow the users viewing of the site.
Question by:Dzynit
    LVL 11

    Expert Comment

    When you say little error icon that leads me to believe this is IE... No you cannot turn that off, the only way to get rid of it is to fix the JavaScript error.... If the user is getting a debug error you can of course turn that off, but only at their local machine, not via PHP....

    The JavaScript error message means you have an error which needs to be fixed, it may not cause a problem now but when the user upgrades to a new browser it may become more of a problem...
    LVL 14

    Author Comment

    I use firefox, but I know both ff and ie have icons that tell you when there is an error.

    Problem is the scripts with errors are not all accessible.

    You can see what I mean here:

    But I don't want to be able to supress them with php, just making the comparison because php has a code line option to supress them. I wondered if javascript had one also.
    LVL 11

    Accepted Solution

    no, JavaScript does not have a suppress command since this is controlled by the web browser and not the code itself... If you use Chrome the error is suppressed but available in the development area...

    Sorry, the error is suppose to alert you to a problem that needs to be corrected, not to just ignore it....  The error I see on the site you listed above is a common one and very easy to fix with either a null check before using the variable or declaring it..
    LVL 18

    Assisted Solution

    You *could* use the try...catch block, that could catch logical errors(but usually not  a syntax error).  If you need to fallback if there is an error, this is how you can.  However, if you are using it simply to mask bad coding, you should fix the code.

    try {
       doThis();  //assuming doThis isn't defined anywhere
    catch(exception) {
       //alert(exception.message);  //doesn't work in all browsers if I recall
       alert('Something borked!');

    Open in new window

    LVL 14

    Author Comment

    One last question if it's common and an easy fix: since it's coming from, how do I fix the error?

    Warning: reference to undefined property jQuery.cache[id][name]
    Source File:
    Line: 19
    LVL 11

    Expert Comment

    Interesting, IE8 shows a different errors....

    Webpage error details

    Message: 'jQuery' is null or not an object
    Line: 1
    Char: 1
    Code: 0

    sounds like jQuery was never declared, I would suggest turning on a JavaScript debugger and stepping through your JavaScript before it calls the Google JavaScript ..

    LVL 22

    Assisted Solution

    how about handling it this way?

        <script type="text/javascript">
        window.onerror = function (msg, url, num) {
            alert(msg + ' || ' + url + ' || ' + num);
            return true;
    	<script type="text/javascript" src="helloworld.js"></script>
        <script type="text/javascript">

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
    #Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
    This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now