如果iframe src无法加载,则会出现捕获错误。错误: - “拒绝在框架中显示

如果iframe src无法加载,则会出现捕获错误。错误: - “拒绝在框架中显示'http://www.google.co.in/'..”

Knockout.js用来绑定iframe src标签(这可以根据用户进行配置)。

现在,如果用户已配置http://www.google.com(我知道它不会在iframe中加载,这就是为什么我将它用于-ve场景)并且必须在IFrame中显示。但它会引发错误: -

拒绝在框架中显示“ http://www.google.co.in/ ”,因为它将“X-Frame-Options”设置为“SAMEORIGIN”。

我为iframe提供了以下代码: -

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

我想要的是,如果网址无法加载。我想显示自定义消息。 骗子在这里

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

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

它可以正常加载w3schools.com,但不能与google.com。

其次: - 如果我把它作为一个功能,并尝试像我在我的小提琴,它不起作用。

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

我不知道如何让它运行并捕获错误。


喵喵时光机
浏览 891回答 3
3回答

慕桂英4014372

onload将始终触发,我解决此问题使用try catch block。当您尝试获取contentDocument时,它将抛出异常。iframe.onload&nbsp;=&nbsp;function(){ &nbsp;&nbsp;&nbsp;var&nbsp;that&nbsp;=&nbsp;$(this)[0]; &nbsp;&nbsp;&nbsp;try{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that.contentDocument; &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;catch(err){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//TODO&nbsp; &nbsp;&nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP