我使用Knockout.js来绑定iframe src标签(这将是可配置的相对于用户).

Now, if user has configured http://www.google.com (I know it won't load in iframe, thats why I am using it for -ve scenario) and that has to be shown in IFrame. but it throws error:-

拒绝在帧中显示"http://www.google.co.in/",因为它 将‘X-Frame-Options’设置为‘SAMEORIGIN’.

I have the following code for Iframe:-

<iframe class="iframe" id="iframe" data-bind="attr: {src: externalAppUrl, height: iframeheight}">
    <p>Hi, This website does not supports IFrame</p>
</iframe>

What I want is, if the URL fails to load. I want to display Custom Message. Screenshot of console when I get error FIDDLE HERE

现在,如果我使用onload和onerror作为:-

<iframe id="browse" style="width:100%;height:100%" onload="alert('Done')" onerror="alert('Failed')"></iframe>

它可以很好地加载w3School s.com,但不能与google.com配合使用.

Secondly:- if I make it as a function and try like I have done in my fiddle, it doesn't works.

<iframe id="browse" style="width:100%;height:100%" onload="load" onerror="error"></iframe>

I don't know how should I make it run and capture the error.

Edited:- I have seen Want to call a function if iframe doesn't load or load's question in stackoverflow but it shows error for sites that can be loaded in iframe.

此外,我还调查了Stackoverflow iframe on load event

推荐答案

由于浏览器设置了Same Origin Policy,因此无法从客户端执行此操作.除了iFrame的宽度和高度等基本属性外,您将无法从iFrame中获得更多信息.

Also, google sets in its response header an 'X-Frame-Options' of SAMEORIGIN.

即使你给谷歌打了一个ajax电话,你也无法判断响应,因为浏览器执行的是同源策略.

因此,唯一的 Select 是从服务器发出请求,查看是否可以在IFrame中显示该站点.

所以,在你的服务器上..您的Web应用程序将向www.google.com发出请求,然后判断响应以查看其头参数是否为X-Frame-Options.如果它确实存在,那么您知道IFRAME将出错.

Jquery相关问答推荐

如何在光滑的轮播上添加进度条

寻找 jQuery 事件类型的完整列表

为什么 jQuery 不使用 requestAnimationFrame?

提醒未保存的表单更改

什么是不使用 jQuery 的经验技术原因?

仅在 _some 字段上通过 Enter 键禁用表单提交

如何使用 jQuery 将分钟转换为小时/分钟并添加各种时间值?

清除并刷新 jQuery Chosen 下拉列表

jQuery 插入 div 作为某个索引

用 jQuery 模拟按键

你如何获得文本区域中的光标位置?

使用 jQuery 拖放防止单击事件

jQuery UI 工具提示不支持 html 内容

您如何处理 jQuery 中的表单更改?

如何检测是否安装了 Flash,如果没有,显示一个隐藏的 div 通知用户?

订单对象是否由指定的 jQuery Select 器返回?

设置 JQuery event.preventDefault() 时绕过 window.open 上的弹出窗口阻止程序

$.ajax 的成功和 .done() 方法有什么区别

由于滚动,响应表内的 bootstrap 按钮下拉菜单不可见

jquery $(window).width() 和 $(window).height() 在未调整视口大小时返回不同的值