在JavaScript中捕捉X-Frame选项错误 [英] Catch X-Frame-Options Error in javascript

查看:115
本文介绍了在JavaScript中捕捉X-Frame选项错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从其他域加载iframe时是否有任何方法可以捕获错误。这是jsfiddle中的一个例子。 http://jsfiddle.net/2Udzu/ 。我需要显示一条消息,如果我收到一个错误。

Is there any way to catch an error when loading an iframe from another domain. Here is an example in jsfiddle. http://jsfiddle.net/2Udzu/ . I need to show a message if I receive an error.

这是我想做的,但它不工作

Here is what I would like to do, but it doesn't work

$('iframe')[0].onerror = function(e) {
   alert('There was an error loading the iFrame');
}

任何人都有任何想法?

推荐答案

onerror 仅适用于脚本错误。帧内容错误检查必须使用任何其他方法完成。这是一个例子。

The onerror is applicable only for script errors. Frame content error checking must be done using any other method. Here's one example.

<script>
  function chkFrame(fr) {
    if (!fr.contentDocument.location) alert('Cross domain');
  }
</script>
<iframe src="http://www.google.com/" onload="chkFrame(this)"></iframe>

由于跨域限制,无法检测页面是否已成功加载或页面由于客户端错误(HTTP 4xx错误)和服务器错误(HTTP 5xx错误),无法加载。

Due to cross domain restriction, there's no way to detect whether a page is successfully loaded or if the page can't be loaded due to client errors (HTTP 4xx errors) and server errors (HTTP 5xx errors).

这篇关于在JavaScript中捕捉X-Frame选项错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆