window.onerror不在Firefox中触发 [英] window.onerror not firing in Firefox

查看:142
本文介绍了window.onerror不在Firefox中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图设置 window.onerror 成为我的错误处理程序。它在IE 6中工作,但是当我在Firefox中运行它时,会遇到一些冲突的 onerror 方法。

  var debug = true; 

MySite.Namespace.ErrorLogger.prototype = {

//我的错误处理函数。
//如果它不处于调试模式,我应该得到一个告警,告诉我这个错误。
//如果是,请给出不同的警报,并让浏览器处理错误。
onError:函数(msg,url,lineNo){
alert('onError:'+ msg);
if(!debug){
alert('not debug mode');
返回true;
}
else {
alert(msg);
返回false;



$ b $ document
$(b $ b $ log $ $ create (MySite.Namespace.ErrorLogger);

window.onerror = log.onError;
$ b $(window).er​​ror(函数(msg,url,line){
log.onError(msg,url,line);
});
});

如果我使用 setTimeout(eval('a'),1 ); 其中' a '是一个未定义的变量,我的错误处理程序是被激发的(它的工作原理)。但是,我的错误记录器需要捕捉访问网站的客户端抛出的所有错误,而不仅仅是错误的代码。



代码位于.js页面从网站的基本页面(C#)被调用。该网站也使用jQuery,所以我有一个函数,重写jQuery绑定函数,并且该功能在Firefox 3和IE 6中都能正常工作。



我知道Firefox错误控制台和Firebug都显示错误,但是我的 window.onerror 函数仍然没有被调用。 p>

关于如何重写Firefox正在做什么的想法?

以下是测试和工作在IE 6和Firefox 3.0.11:

 < html> 
< head>
< title>标题< / title>
< / head>
< body>
< script type =text / javascript>
window.onerror =函数(msg,url,num){
alert(msg +';'+ url +';'+ num);
返回true;
}
< / script>
< div>
...内容...
< / div>
< script type =text / javascript>
等等;
< / script>
< / body>
< / html>

如果您正在加载的其他JavaScript库也附加到窗口。

 < script type =text / javascript> 
函数addHandler(obj,evnt,handler){
if(obj.addEventListener){
obj.addEventListener(evnt.replace(/ ^ on /,''),handler,false) ;
//注意:attachEvent以与
//相连的相反顺序触发处理程序。这与addEventListener
//和手动附件是相反的。
//} else if(obj.attachEvent){
// obj.attachEvent(evnt,handler);
} else {
if(obj [evnt]){
var origHandler = obj [evnt];
obj [evnt] = function(evt){
origHandler(evt);
处理程序(evt);

} else {
obj [evnt] = function(evt){
handler(evt);

$ b $ addHandler(window,'onerror',function(msg,url,num){
alert(msg +') ;'+ url +';'+ num);
return true;
});
addHandler(window,'onerror',function(msg,url,num){
alert('and again'+ msg +';'+ url +';'+ num);
返回true;
});
< / script>

上面的代码可以添加 onerror 处理程序,如你所愿。如果已经存在一个自定义的 onerror 处理函数,它将会调用这个函数,然后是你的。



  addHandler (window,'onload',function(){alert('one');}); 
addHandler(window,'onload',function(){alert('two');});
addHandler(window,'onload',function(){alert('three');});

这段代码是新的,有点实验性。我不能100%确定 addEventListener 的确是手动附件的功能,作为评论, attachEvent 以相反的顺序触发处理程序(所以你会在上面的例子中看到三,二,一)。虽然不一定是错误的或不正确的,但与 addHandler 中的其他代码相反,可能导致浏览器与浏览器之间的行为不一致,这就是为什么我删除它。

编辑:

这是一个完整的测试用例来演示onerror事件:

 < html> 
< head>
< title>标题< / title>
< / head>
< body>
< script type =text / javascript>
函数addHandler(obj,evnt,handler){
if(obj.addEventListener){
obj.addEventListener(evnt.replace(/ ^ on /,''),handler,false) ;
} else {
if(obj [evnt]){
var origHandler = obj [evnt];
obj [evnt] = function(evt){
origHandler(evt);
处理程序(evt);

} else {
obj [evnt] = function(evt){
handler(evt);

$ b $ addHandler(window,'onerror',function(msg,url,num){
alert(msg +') ;'+ url +';'+ num);
return true;
});
< / script>
< div>
...内容...
< / div>
< script type =text / javascript>
等等;
< / script>
< / body>
< / html>

当上面的代码放在test.htm中并从本地idks加载到Internet Explorer时,应该看到一个对话框,说'blah'是undefined; undefined; undefined



当上面的代码是放入test.htm并从本地磁盘加载到Firefox 3.0.11(以及此编辑的最新3.5版本 - Gecko / 20090616)中,应该会看到一个对话框,其中显示 [object Event];未定义的;未定义。如果没有发生,那么你的Firefox副本配置不正确或以其他方式破坏。我只能建议你删除Firefox,删除你的本地配置文件(有关如何找到你的配置文件的信息是可用的 here )并重新安装最新的版本并再次测试。


I'm trying to create a javascript error logging infrastructure.

I'm trying to set window.onerror to be my error handler. It works in IE 6, but when I run it in Firefox, it runs into some conflicting onerror method.

var debug = true;

MySite.Namespace.ErrorLogger.prototype = {

   //My error handling function.  
   //If it's not in debug mode, I should get an alert telling me the error.
   //If it is, give a different alert, and let the browser handle the error.
   onError: function(msg, url, lineNo) {
        alert('onError: ' + msg);
        if (!debug) {
            alert('not debug mode');
            return true;
        }
        else {
            alert(msg);
            return false;
        }
    }
}

//Document ready handler (jQuery shorthand)
$(function() {
    log = $create(MySite.Namespace.ErrorLogger);

    window.onerror = log.onError;

    $(window).error(function(msg, url, line) {
        log.onError(msg, url, line);
    });
});

If I use setTimeout("eval('a')", 1); where 'a' is an undefined variable, my error handler is what's fired (it works). However, my error-logger needs to catch all errors thrown by clients accessing the website, not just incorrect code in one place.

The code is on a .js page that is being called from the base page (C#) of a website. The site also uses jQuery, so I have a function that overrides the jQuery bind function, and that function works fine in both Firefox 3 and IE 6.

I know that Firefox is seeing the error because it shows up in both the Error Console and Firebug, but my window.onerror function is still not being called.

Any thoughts on how to override what Firefox is doing?

解决方案

The following is tested and working in IE 6 and Firefox 3.0.11:

<html>
<head>
<title>Title</title>
</head>
<body>
    <script type="text/javascript">
    window.onerror = function (msg, url, num) {
        alert(msg + ';' + url + ';' + num);
        return true;
    }
    </script>
    <div>
    ...content...
    </div>
    <script type="text/javascript">
    blah;
    </script>
</body>
</html>

If some other JavaScript library you are loading is also attaching itself to window.onerror you can do this:

<script type="text/javascript">
function addHandler(obj, evnt, handler) {
    if (obj.addEventListener) {
        obj.addEventListener(evnt.replace(/^on/, ''), handler, false);
    // Note: attachEvent fires handlers in the reverse order they
    // were attached. This is the opposite of what addEventListener
    // and manual attachment do.
    //} else if (obj.attachEvent) {
    //    obj.attachEvent(evnt, handler);
    } else {
        if (obj[evnt]) {
            var origHandler = obj[evnt];
            obj[evnt] = function(evt) {
                origHandler(evt);
                handler(evt);
            }
        } else {
            obj[evnt] = function(evt) {
                handler(evt);
            }
        }
    }
}
addHandler(window, 'onerror', function (msg, url, num) {
    alert(msg + ';' + url + ';' + num);
    return true;
});
addHandler(window, 'onerror', function (msg, url, num) {
    alert('and again ' + msg + ';' + url + ';' + num);
    return true;
});
</script>

The above lets you attach as many onerror handlers as you want. If there is already an existing custom onerror handler it will invoke that one, then yours.

Note that addHandler() can be used to bind multiple handlers to any event:

addHandler(window, 'onload', function () { alert('one'); });
addHandler(window, 'onload', function () { alert('two'); });
addHandler(window, 'onload', function () { alert('three'); });

This code is new and somewhat experimental. I'm not 100% sure addEventListener does precisely what the manual attachment does, and as commented, attachEvent fires the handlers in the reverse order they were attached in (so you would see 'three, two, one' in the example above). While not necessarily "wrong" or "incorrect", it is the opposite of what the other code in addHandler does and as a result, could result in inconsistent behaviour from browser to browser, which is why I removed it.

EDIT:

This is a full test case to demonstrate the onerror event:

<html>
<head>
<title>Title</title>
</head>
<body>
<script type="text/javascript">
function addHandler(obj, evnt, handler) {
    if (obj.addEventListener) {
        obj.addEventListener(evnt.replace(/^on/, ''), handler, false);
    } else {
        if (obj[evnt]) {
            var origHandler = obj[evnt];
            obj[evnt] = function(evt) {
                origHandler(evt);
                handler(evt);
            }
        } else {
            obj[evnt] = function(evt) {
                handler(evt);
            }
        }
    }
}
addHandler(window, 'onerror', function (msg, url, num) {
    alert(msg + ';' + url + ';' + num);
    return true;
});
</script>
<div>
...content...
</div>
<script type="text/javascript">
blah;
</script>
</body>
</html>

When the above code is put in test.htm and loaded into Internet Explorer from the local idks, you should see a dialog box that says 'blah' is undefined;undefined;undefined.

When the above code is put in test.htm and loaded into Firefox 3.0.11 (and the latest 3.5 as of this edit - Gecko/20090616) from the local disk, you should see a dialog box that says [object Event];undefined;undefined. If that is not happening then your copy of Firefox is not configured correctly or otherwise broken. All I can suggest is that you remove Firefox, remove your local profile(s) (information about how to find your profile is available here) and reinstall the latest version and test again.

这篇关于window.onerror不在Firefox中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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