在javascript中捕获X-Frame-Options错误 [英] Catch X-Frame-Options Error in javascript

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

问题描述

从另一个域加载 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-Options错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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