模仿窗口。使用javascript在Opera中的错误 [英] Mimic Window. onerror in Opera using javascript

查看:53
本文介绍了模仿窗口。使用javascript在Opera中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Web应用程序,我有一个JS日志记录机制来处理未被页面内的js代码捕获的Javascript错误。我正在使用window.onerror来捕获所有这些错误并将它们记录在其他位置。

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.

然而,问题在于Opera没有window.onerror事件。我能想到的一种方法是,对所有js函数进行字符串处理,然后在body加载后在这些函数中插入try catch块。但它在许多情况下都不起作用,但是,它至少在一定程度上起作用。

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);}';
        }
    }
}


推荐答案

这也发生在Safari,AFAIK上。

This also happens on Safari, AFAIK.

你当然可以为所有JS代码而不是文本解析创建一个全局try / catch块 - 如果你遇到这样的事情可能会很棘手:

What you could certainly do is create a global try/catch block for all JS code instead of text parsing - which could be tricky if you come into things like:

(function ($){
  $(function (){
  });
})(jQuery);

这篇关于模仿窗口。使用javascript在Opera中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆