Skip to content
Advertisement

Suppress JavaScript undefined errors?

I have written a script that checks a set of radiobuttons to be checked. But due to different possibilities different radiobuttons will show. Is there a way to suppress JavaScript errors when it pops undefined/getElementById is null? Something like the @-char does in PHP?

Update:

A bit more background info. I’ve made a website where users can submit images and another party for whom the images are can select their top 3 of the images. So each image has three radiobuttons. The difficulty here lies in the fact that the radiobuttons must be controlled dimensional (horizontal and vertical), because a submitted image may only be at place 1, 2 or 3. This is my working code. But adding many if(!var == undefined) doesn’t make the code prettier. Therefor I’m wondering if there is something like @suppressMe is possible?

function HandleRadioButtons(id, type, idString, img)
{
    var idArray = idString.split("|");  
    var place1  = document.getElementById("G_" + id);
    var place2  = document.getElementById("S_" + id);
    var place3  = document.getElementById("B_" + id);
    var img1    = document.getElementById("Winner1");
    var img2    = document.getElementById("Winner2");
    var img3    = document.getElementById("Winner3");    

    switch(type)
    {
        case "G" :
            place2.checked = false;
            place2.disabled = true;
            place3.checked = false;
            place3.disabled = true;
            img1.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
        case "S" :
            place1.checked = false;
            place1.disabled = true;
            place3.checked = false;
            place3.disabled = true;
            img2.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
        case "B" :
            place1.checked = false;
            place1.disabled = true;
            place2.checked = false;
            place2.disabled = true;
            img3.style.background = 'url(' + img + ') no-repeat center center #FFF';
            break;
    }     

    var current1, current2, current3 = "";

    for(i = 0; i < idArray.length - 1; i++)
    {
        var place1 = document.getElementById("G_" + idArray[i]);
        var place2 = document.getElementById("S_" + idArray[i]);
        var place3 = document.getElementById("B_" + idArray[i]);

        if(place1.checked == true)
        {
            var current1 = idArray[i];            
        }

        if(place2.checked == true)
        {
            var current2 = idArray[i];            
        }

        if(place3.checked == true)
        {
            var current3 = idArray[i];            
        }
    }

    for(i = 0; i < idArray.length - 1; i++)
    {        
        var place1 = document.getElementById("G_" + idArray[i]);
        var place2 = document.getElementById("S_" + idArray[i]);
        var place3 = document.getElementById("B_" + idArray[i]);

        if(idArray[i] != id && idArray[i] != current1 && idArray[i] != current2 && idArray[i] != current3)
        {
            switch(type)
            {
                case "G" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
                case "S" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
                case "B" :
                    place1.disabled = false;
                    place2.disabled = false;
                    place3.disabled = false;
                    break;
            }
        }
    }   
}

Advertisement

Answer

You can easily test for a null or undefined value in JavaScript, as both these values are falsy:

var element = document.getElementById('some-id');
if (element) {
   element.value = 'Hello';
}

You could also consider using a try/catch block:

try {
   var element = document.getElementById('some-id');
   element.value = 'Hello';

   // ... the rest of your code here.
}
catch (e) {
   if (!(e instanceof TypeError)) {
      // The exception is not a TypeError, so throw it again.
      throw e;
   }
}

However be careful that the above will suppress all the TypeError exceptions and that might make your code more difficult to debug.

Advertisement