6

I know one can add event listener for window.error.

However when working with Iframes, each iframe has its own window element, and window.error should be created for each and every iframe.

Is it possible somehow to define error event handler in one location, where all errors will trigger this specific method?

2 Answers 2

7

This might work.

function myHandler(msg, url, line){
  //do stuff here...
}

//hook in all frames...
function addErrorHandler(win, handler){
  win.onerror = handler;
  for(var i=0;i<win.frames.length;i++){
    addErrorHandler(win.frames[i], handler);
  }
}
//start with this window... and add handler recursively
addErrorHandler(window, myHandler);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that. looks nice, although seems not to be perfect for my app - IFrames and windows are created on the fly without me able to control much of them, so I cannot know in advance all the win objects. I guess there isn't a global error handling way in IE window? Thanks, Tal.
ah, in that case... in every frame you create, just add... window.onerror = top.myHandler;
yes...but I don't know in advance what would be the source of the Iframe. (using form submit to the target IFrame) - the window.onerror should be inside the IFrame code itself..
Hmm, do you have a global JS file that you include in all frames? if so, can your onerror handler go in there? and be hooked in to each window when the script file is loaded? Depending what info you want/need in the error handler, you could check the parent[n], and child[n] frames to determine hierarchy within the error handler. Out of interest... what do you do with the handler? e.g. do you do an AJAX callback to log the error/email system support? or do you just alert the user in a "pretty" way?
There are several Iframes with a global JS and this is a good idea. Some Iframes are do not have anything in common and created dynamically - any thoughts for that? Regarding the error handler - I'm thinking of doing some AJAX callback for support. but currently this is saved only on client side in some string for later viewing.
1

I haven't tried this so please don't hang me for it :-) In the master/parent window that holds all of the iframes, you could create your error handing function there. Then use jQuery to grab all of your iFrames in your page and register the .error handler to point to your function registered in the parent window.

PS: Also while were on the topic of javascript error handling, this is pretty cool too: https://damnit.jupiterit.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.