Di seguito è provato e funzionando in IE 6 e 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>
Se qualche altra libreria JavaScript che si sta caricando è anche attaccandosi a window.onerror
si può fare questo:
<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>
Quanto sopra consente di allegare quanti più onerror
gestori come si desidera. Se esiste già un gestore personalizzato onerror
esistente, invocherà quello, quindi il tuo.
noti che addHandler()
può essere utilizzato per legare più gestori a qualsiasi evento:
addHandler(window, 'onload', function() { alert('one'); });
addHandler(window, 'onload', function() { alert('two'); });
addHandler(window, 'onload', function() { alert('three'); });
Questo codice è nuova e un po 'sperimentale. Io non sono sicuro al 100% addEventListener fa esattamente ciò che l'allegato manuale fa, e come ha commentato, attachEvent incendi gli handler nell'ordine inverso sono stati attaccati in (così si vedrebbe 'tre, due, uno' nell'esempio di cui sopra). Anche se non necessariamente "sbagliato" o "errato", è l'opposto di quello che fa l'altro codice in addHandler
e, di conseguenza, potrebbe comportare un comportamento incoerente dal browser al browser, motivo per cui l'ho rimosso.
EDIT:
Questo è un banco di prova completo per dimostrare l'evento onerror:
<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>
Quando il codice di cui sopra è messo in Test.htm e caricato in Internet Explorer dal idks locali, è dovrebbe vedere una finestra di dialogo che dice 'blah' is undefined;undefined;undefined
.
Quando il codice di cui sopra è messo in Test.htm e caricato in Firefox 3.0.11 (e l'ultima 3.5 a partire da questa modifica - Gecko/20090616) dal disco locale, dovresti vedere una finestra di dialogo che dice [object Event];undefined;undefined
. Se ciò non accade, la tua copia di Firefox non è configurata correttamente o altrimenti non funzionante. Tutto quello che posso suggerire è che tu rimuovi Firefox, rimuovi i tuoi profili locali (le informazioni su come trovare il tuo profilo sono disponibili here) e reinstallano la versione più recente e testano nuovamente.
Qualcun altro nel mio gruppo ha assunto questo progetto e ha completamente cambiato tutto ciò che stavo facendo, quindi non saprò mai cosa c'è che non va. –