如何处理 iframe 内的 javascript 错误?
我有一个网站,其中没有发生 JavaScript 错误,对此我感到非常自豪。新的要求使我必须在我的网站上放置一个 iframe,以显示不同域的其他人的网站,并且我无法访问他们的代码。他们的 JavaScript 不断抛出错误,导致用户在浏览器底部看到一个丑陋的红色 x。
我是否可以处理该错误并忽略它?
更新:
简而言之,我正在尝试找到一种方法来劫持 iframe 的 window.onerror 处理程序。
更新:
我不相信这里有答案。即使我可以劫持 iframe onerror 事件,我也不认为有办法让丑陋的红色“X”消失。我将问题留在这里,希望我是错的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你基本上问的(我认为)是“我可以在 IFRAME 的代码周围放置一个 try/catch 吗”,答案是否定的。然而,至少在许多浏览器中(不是 Opera),您可以通过使用 window.onerror 来接近(并且由于 IFRAME 有它自己的窗口,您应该能够使用此技术来仅捕获 IFRAME 的错误)。
看:
Javascript 全局错误处理
欲了解更多信息。
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 come 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.