我已经设置了一个window.onerror处理程序来捕获错误.它的目的?要在每次发生错误时连接一个字符串,然后另一个处理程序window.onunload将其发送到服务器. (据推测有所有错误).
问题是,它只能发送第一个错误,因为在第一次触发onerror处理程序事件后,错误没有被捕获,因此脚本终止…
有没有解决的办法?
ErrorManager: (function () { function Init(message) { InitErrorHandler(); InitAjaxHandler(); setTimeout("Interface.ErrorManager.SendErrorsToAjax()",8000); } function InitErrorHandler() { Data.ErrorHandlerText = ""; Data.ErrorHandlerCount = 0; window.onerror = function(errorMessage,url,line) { if(IsInIgnoreList(errorMessage)) { return; } Data.ErrorHandlerText += ("Error: "+(Data.ErrorHandlerCount+1)+" <br /><br />"); //Get error specific info Data.ErrorHandlerText += escape(errorMessage) + "<br />"; Data.ErrorHandlerText += escape(url) + "<br />"; Data.ErrorHandlerText += escape(line) + "<br />"; Data.ErrorHandlerCount++; console.log("BOOM"+errorMessage); } } function InitAjaxHandler() { window.onbeforeunload = function() { //when browser closed Data.ErrorHandlerCount > 0 && SendErrorsToAjax(); } } function SendErrorsToAjax() { PrepareErrorsForAjax(); $.getJSON(Interface.Utility.PrefixURL('/ajax/sendFeedback/handlejserrors/'+Data.ErrorHandlerText)); } function IsInIgnoreList(str) { return str.indexOf('nsIDOMSVGLocatable') != -1 ? true : false; } function PrepareErrorsForAjax() { var preText = "<br /><br />A user has encountered a few errors: <br /><br />"; //Get session info var userAgent,activePageID,accountNO,consumerNO; userAgent = escape(navigator.userAgent); preText += "User agent: "+userAgent+" <br />"; if($.mobile.activePage) { activePageID = $.mobile.activePage.attr('id'); preText += "Page ID: "+activePageID+" <br />"; } //Get info that may or may not be set console.log("AUTH JSON "+Data.authJSON); console.log("CONSUMER JSON "+Data.authJSON.consumers); if(Data.authJSON && localStorage.lastConsumerNo) { preText += "Account Number: " +Data.authJSON.accountId+" <br />"; preText += "Consumer Number: "+ localStorage.lastConsumerNo+" <br />"; } preText += "<br /> The following errors were encountered:<br /><br />"; Data.ErrorHandlerText = preText + Data.ErrorHandlerText; } return { Init: Init,SendErrorsToAjax: SendErrorsToAjax } })(),