ページ上ですべてのjavascriptエラーを自分自身に送信したいと考えています。私は拡張開発者です。したがって、以下は、domを呼び出す前に準備が整っていることを確認することに重点を置いています。ページ上のすべてのjavascriptエラーを取得する/ javascriptエラー処理
throw
にいくつかの機能を追加して、例外を送信したり郵送したりすることを検討しましたが、これは可能ではありませんでした。
1:このメインソリューション、window.onerrorハンドラ:
window.onerror = function(e, url, line){
mailError('onerror: ' + e + ' URL:' + url + ' Line:' + line);
console.error('\nLine ' + line + ':');
setTimeout(function(){retry();}, 100); //mainly useful in content scripts for extensions,
return true;
}
もjQueryを介して、同じことを行う必要があるかもしれません:
$(window).error(
function(e, url, line){
//handle error
}
);
は、このの没落は、あなたのことですコードの実行が停止しました。
エラーを回避して実行を停止するには、コールバックを使用してコードが特定の順序で実行されるようにし、可能な限りイベントを利用することをお勧めします。また、あなたはまた、これらの技術を使用してwindow.onload
window.onload = function(){
//you page will probably twitch upon executing code at this time
//but you will almost never have a bad dom call
};
別の技術
if (document.getElementById('iNeedThisElement')) {
//doin work!
document.getElementById('iNeedThisElement').style.display = 'block';
} else {
var stillNeedToTakeCareOfX = true; //false otherwise since it's undefined
mailError('iNeedThisElement was unavailable...');
}
上でコードを実行することを検討することができます
$(document).ready({
//you usually won't get bad dom calls wrapping your code in this
});
いくつかのテクニックとDOMコールを保護し、ちょうど、デバッグすることができますアプリケーションでは、かなり上手くいくはずです。 console.error、.WARN、および.logの文が取得され、戻ってあなたに報告することができないので、小さな代替スイートは下方に設けられている。土壇場のよう
var Xe = { }; //Extreme error suite
function extraInfo(e){
//e optional
if(!e)e = true;
//add an extra debug info, such as navigator or current URL
return ' currentURL: '+ document.URL +
'\n userAgent: ' + navigator.userAgent +
'\n platform: ' + navigator.platform +
'\n userid: ' + localStorage.userid +
'\n language: ' + navigator.langauge +
'\n cookies?: ' + navigator.cookiesEnabled;
}
Xe.error = function(e){
console.error(e); //maintain original functionality
mailError('Xe err: ' + e + extraInfo());
}
Xe.warn = function(e){
console.warn(e);
mailError('Xe warn: ' + e + extraInfo());
}
Xe.log = function(e){
console.log(e);
mailError('Xe log: ' + e + extraInfo());
}
、あなたは絶えずのチャンクを実行しようとすることができますエラーなしで実行されるまでこれは下の「2」です。
2:グループ大きなチャンク内のコードおよびそれ以降のエラーをキャッチした後、X秒を再実行し、またはそれは次いで
//array to group function chunks
var fn = [ ];
fn[1] = function(){
//a large chunk of javascript
}
defExe(fn[1]);
fn[2] = function(){
//another chunk of your program
}
defExe(fn[2]);
ように使用されるコードの次のチャンク
//defExe = DEFinitely EXEcute this code
//functionArg, reference to a function holding a large chunk of code
//seconds, timeout in milliseconds to re-attempt error free execution of this function
function defExe(functionArg, seconds) {
//seconds is optional
if (!seconds)seconds = 300;
try {
functionArg();
} catch(e) {
//mail the error plus extra info
mailError('caught ' + e + ' attempting to re-execute ' + functionArg.name + ' in ' + seconds + ' milliseconds');
//re-attempt to execute this code
setTimeout(function(){
defExe(functionArg, seconds);
}, seconds);
}
}
これを続けるに試み
#2概要:機能のグループコードと、try-catchブロックで実行私は私がwindow.onerrorで作業していたエラーが
'fn.1'はシンタクスが無効です。 'fn [1]'を使う必要があります。 – HoLyVieR
実際に私は、私がしたいのは、 "DOMがgetElementByIdに応答する"ときに発生するYUI利用可能なイベントであることを知ったと思います。http://developer.yahoo.com/yui/3/event/#onavailable 今私は、このYUIコード機能でDOM呼び出しをラップする、より巧みなdoc.byId関数を作成する必要があります。しかし、私はエラーを得るのを失うかもしれないので、まだ動いていないコードについては知りません。 –