admin管理员组

文章数量:1389749

I have a page on which mysterious JavaScript errors keep popping up. They appear to be ing from the application we use and do not own the source for. I'm working on a real solution to this issue, but we have a demo tomorrow and I was wondering if there is a way to just suppress JS errors page wide (like wrapping ALL the javascript ponents in a giant try catch).

I have a page on which mysterious JavaScript errors keep popping up. They appear to be ing from the application we use and do not own the source for. I'm working on a real solution to this issue, but we have a demo tomorrow and I was wondering if there is a way to just suppress JS errors page wide (like wrapping ALL the javascript ponents in a giant try catch).

Share Improve this question asked Jan 31, 2011 at 2:49 MaxxMaxx 4,0828 gold badges34 silver badges38 bronze badges 5
  • 1 What's a giant try catch going to do? It'll make your code fail silently, not work correctly. – Matt Ball Commented Jan 31, 2011 at 2:50
  • @Matt well, if it works for the demo... See more info here. A giant try/catch seems to be the tool of choice – Pekka Commented Jan 31, 2011 at 2:52
  • 1 @Pekka: eh, I guess... seems rather weak. – Matt Ball Commented Jan 31, 2011 at 2:53
  • 2 @Matt, sed 's/seems rather/is totally/' – ocodo Commented Jan 31, 2011 at 3:24
  • 2 I would not use one giant try-catch. It will make identifying and fixing your bugs (or even their bugs) so much harder. Consider using a wrapper around said API that you can control and wrap the individual actions (may be object-based or operation-based). – user166390 Commented Jan 31, 2011 at 3:27
Add a ment  | 

1 Answer 1

Reset to default 10

You could add a handler to the window.onerror event. In this case, all the errors that occur inside the window will be redirected to the handler of this event. (I did test this in Firefox and it worked, but I was having trouble with it in Chrome - my Chrome installation is pretty messed up, so that could be the problem, but there are Chromium bugs filed that relate to this issue: bug #7771 and bug #8939)

window.onerror = function (msg, url, line) {
    alert("Error on line " + line + " in " + url + ":\n" + msg);
    // return true to prevent browser from displaying error
    return true;
}

本文标签: javascriptIs there a way to trycatch an entire page dynamicallyStack Overflow