最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to handle javascript errors inside an iframe? - Stack Overflow

programmeradmin3浏览0评论

I have a site in which I take great pride in the fact that no javascript errors happen. New requirements make me have to put an iframe on my site which displays someone else's site of a different domain, and I have no access to their code. Their javascript consistently throws errors which causes users to see an ugly red x in the bottom of their browser.

Is it possible to handle that error on my end and disregard it?

Update:
In short, I'm trying to find a way to hijack the iframe's window.onerror handler.

Update:
I don't believe there is an answer here. Even if I could hijack the iframe onerror events, I don't think there's a way to make the ugly red 'X' go away. I'll leave the question here in hopes that I'm wrong.

I have a site in which I take great pride in the fact that no javascript errors happen. New requirements make me have to put an iframe on my site which displays someone else's site of a different domain, and I have no access to their code. Their javascript consistently throws errors which causes users to see an ugly red x in the bottom of their browser.

Is it possible to handle that error on my end and disregard it?

Update:
In short, I'm trying to find a way to hijack the iframe's window.onerror handler.

Update:
I don't believe there is an answer here. Even if I could hijack the iframe onerror events, I don't think there's a way to make the ugly red 'X' go away. I'll leave the question here in hopes that I'm wrong.

Share Improve this question edited Oct 11, 2011 at 18:06 tybro0103 asked Oct 7, 2011 at 19:19 tybro0103tybro0103 49.8k34 gold badges148 silver badges172 bronze badges 2
  • Does the other site have some JSON way of getting data? – Detect Commented Oct 7, 2011 at 19:23
  • @Detect I'm not sure what exactly you're asking, but I don't have any way to municate with the site or have any control over it. – tybro0103 Commented Oct 7, 2011 at 19:28
Add a ment  | 

1 Answer 1

Reset to default 5

What you're basically asking (I think) is "can I put a try/catch around the IFRAME's code", and the answer is no. However, you can e close, in many browsers at least (not Opera) by using window.onerror (and since the IFRAME has it's own window, you should be able to use this technique to capture only the IFRAME's errors).

See: Javascript global error handling for further info.

发布评论

评论列表(0)

  1. 暂无评论