10

如果图像无法加载到页面上,我们有 onerror 函数可以加载默认图像。

是否有将网站加载到 iFrame 的等价物?

例如 mashable.com 可以加载到 iFrame 中,而许多社交网站(例如 Facebook、Twitter 等)则不能。

因此,当用户尝试加载例如 Twitter 并且没有显示任何内容时,我想要一条消息显示“无法显示此页面”,然后在新选项卡中打开链接并在 3 秒后引导它们。

不确定这是否可能。

4

5 回答 5

4

由于安全限制,这在客户端是不可能的。但是,您有两种替代解决方案:

  • 从服务器调用并检查 X-Frame-Options 标头。
  • 或者,您可以设置超时并假设如果加载事件在一段时间后未触发,则无法加载页面。

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

于 2014-05-09T20:07:51.083 回答
2

我有同样的问题,我正在使用 AngularJS,我使用下面的方法让它工作。希望能帮助到你..

  1. 在 html 中设置 iframe

    <div ng-if="!iframeError">
      <iframe id="iframeID" src="http://www.google.com" height="500px"></iframe>
    </div>
    <div ng-if="iframeError">Not Supported</div>
    
  2. 如果 iframe 可用,我会设置监听间隔

    var ife = setInterval(function () {
    if ($("#iframeID")) {
     $("#iframeID").load(function (ev) {
      var iframe = $("#iframeID");
      var iframeDocument;
      try {
      iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
      }
      catch (e) {
       window.open("http://www.google.com");
       $scope.iframeError = true;
       //I opened the url in new page
       //you can handle it any way you want
      }
     });
     clearInterval(ife);
     }
    }, 200);
    
于 2017-01-28T17:33:30.493 回答
0

You can wrap the code around:

if($("#iframe1").find('iframe').length > 0) {
    //Site allowed
}
else {
    //Site blocked.
}
于 2014-04-16T07:54:42.623 回答
0
var isInIframe = (window.location != window.parent.location);

if (isInIframe) {
    alert("Window is in iframe");
    // Do what you want
    //top.location.href=self.location;
} else {
    alert("Window is not in iframe");
}
于 2014-07-06T10:21:56.480 回答
0

if(frames){if(top.frames.length>0){ YOUR CODE HERE }}

如果要删除父级:

if(frames){if(top.frames.length>0){top.location.href=self.location;}}

于 2013-10-10T13:36:17.540 回答