views:

43

answers:

1

I wrote a bookmarklet, when the user will click on it in his browser it will do some actions on the current website and show some results in a popup.

In some websites my bookmarklet fails to load because problems may exist in the current website.

My bookmarklet code is using the jquery library.

In some cases the errors happens in the jquery library because some conflicts or something i don't know.

My Question is:
Can i surround all my bookmarklet code with one try catch which will catch any error happen in all my code and redirect the user to another page 'error page'?

A: 

Errors will bubble down the stack towards the original calling function, so if you wrap your entire top-level code with a try...catch then jQuery errors will be caught.

http://jsfiddle.net/NcEpe/

Ian Wetherbee
but i don't mean errors i create, i mean errors raised by the browser.
Amr ElGarhy
Can you give an example error/backtrace from firebug? All errors in your plugin will be originated by your code, so they can all be caught by a try...catch. To catch errors caused on the page, see: http://stackoverflow.com/questions/205688/javascript-exception-handling/205982#205982
Ian Wetherbee
window.onerror worked for my case, thanks.
Amr ElGarhy
i will accept this answer because of the comment you sent on it about the other answer. but i still have a problem with window.onerror with safari, but will handle this later.
Amr ElGarhy