I can't figure out why prototype suppressess the error messages in the dom:loaded
event, and in AJAX handlers.
Given the following piece of HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Conforming XHTML 1.1 Template</title>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
document.observe('dom:loaded', function() {
console.log('domready');
console.log(idontexist);
});
</script>
</head>
<body>
</body>
</html>
The domready event fires, I see the log in the console, but there is no indication of any errors whatsoever. If you move the console.log(idontexist);
line out of the handler, you get the
开发者_StackOverflow社区idontexist is not defined
error in the console.
I find it a little weird, that in other event handlers, like 'click', you get the error message, it seems that it's only the dom:loaded
that has this problem.
The same goes for AJAX handlers:
new Ajax.Request('/', {
method: 'get',
onComplete: function(r) {
console.log('xhr complete');
alert(youwontseeme);
}
});
You won't see any errors. This is with prototype.js 1.6.1, and I can't find any indication of this behavior in the docs, nor a way to enable error reporting in these handlers.
I have tried stepping through the code with FireBug's debugger, and it seems to jump to a function on line 53 named K, when it encounters the missing variable in the dom:loaded
handler:
K: function(x) { return x }
But how? Why? When? I can't see any try/catch block there, how does the program flow end up there?
I know that I can make the errors visible by packing my dom:ready
handler(s) in try/catch blocks, but that's not a very comfortable option. Same goes for registering a global onException handler for the AJAX calls.
Why does it even suppress the errors? Did someone encounter this before?
after a while i found that prototype redirects all exceptions to onException handler:
new Ajax.Request('/ajax_html_echo', {
method: 'get',
onComplete: function(r) {
console.log('xhr complete');
alert(undefinedVar)
},
onException: function(request,e){
console.log(e.message); // prints undefinedVar is not defined
}
});
more info here
http://www.prototypejs.org/api/ajax/options
onException Triggered whenever an XHR error arises. Has a custom signature: the first argument is the requester (i.e. an Ajax.Request instance), the second is the exception object.
You can also re-throw the exception from onException and you'll get it in the same way as if it happened outside the call
onException: function(request,e){throw e;}
Worked for me
Ajax.Responders
A repository of global listeners notified about every step of Prototype-based Ajax requests.
http://api.prototypejs.org/ajax/Ajax/Responders/
Ajax.Responders.register({
onCreate: function() {
Ajax.activeRequestCount++;
},
onComplete: function() {
Ajax.activeRequestCount--;
}
});
精彩评论