Catch iframe exceptions

Viewed 10365

When I set the src attribute on an iframe, sometimes an error is logged to the console (seems to be an uncaught exception), and the destination never loads.

For example, if I set myiframe.src = 'http://www.google.com', it just never loads and my Chrome developer tools will log the following message:

Refused to display document because display forbidden by X-Frame-Options.

I understand what this error is, but I want to catch this exception so I can handle it when a website doesn't want to be loaded into an iframe.

Because setting an iframe src is semi-asynchronous, I can't just surround that line with a try catch.

How do I handle iframe errors? iframe.onerror and iframe.onabort don't seem to do the trick either.

1 Answers
Related