Mimic Window. onerror in Opera using javascript

kalyang picture kalyang · Mar 14, 2009 · Viewed 8.1k times · Source

I am currently working on a web application, I have a JS logging mechanism that Handles Javascript error that are not caught by the js code inside the page. I am using window.onerror to catch all such errors and log them else where.

However, Problem is with Opera which does not have window.onerror event. one approach I could think of is, to string process all js functions code and insert try catch blocks inside those functions after body load. It does not work in many cases though, But, It at least works to some extent.

I am sure this approach sucks, But, I could not think of anything better. Please advise.

Update: For now, I am calling the code below to Catch as many errors as I could.

function OnBodyLoad()
{
        var allElements = document.getElementsByTagName("*");
        for(var cnt = 0;cnt < allElements.length;cnt++)
        {
            RegisterAllEvents(allElements[cnt]);
        }
}
function RegisterAllEvents(objToProcess){
    for(var cnt = 0;cnt < objToProcess.attributes.length;cnt++){
        if(IsAttributeAnEvent(objToProcess.attributes[cnt].name))
        {
            objToProcess.attributes[cnt].value =  'try{'+objToProcess.attributes[cnt].value+'}catch(err){LogError("'+ objToProcess.id+'"'+ ',err);}';
        }
    }
}

Answer

Kornel picture Kornel · Mar 15, 2009

Opera 11.60+ supports window.onerror.

Opera's Dragonfly supports remote debugging. You might be able to hack it (it's all written in JavaScript) and log errors yourself (unfortunately the protocol isn't published yet).