【问题标题】:Catch set 'X-Frame-Options' to 'sameorigin error in js在 js 中将“X-Frame-Options”设置为“同源错误”
【发布时间】:2018-05-10 12:06:56
【问题描述】:

我在 iframe 中加载了一些页面。 我得到错误

Refused to display 'url' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

如何在 javascript 中处理此类错误,并针对这种情况显示我的消息? window.onerror 没有发现这个错误。 有没有办法做到这一点?

【问题讨论】:

标签: javascript iframe


【解决方案1】:

DOMException 在这里提供帮助。

我有一个技巧如下:[尚未测试跨浏览器!]

定义 iframe 的 onload 事件处理程序定义为

$('#myIframe').on('load', function() {
    setTimeout(function() {
        try {
            console.log($('#myIframe')[0].contentWindow.document);
        } catch (e) {
            console.log(e);
            if (e.message.indexOf('Blocked a frame with origin') > -1 || e.message.indexOf('from accessing a cross-origin frame.') > -1) {
                alert('Same origin Iframe error found!!!');
                //Do fallback handling if you want here
            }
        }
    }, 1000);

});

【讨论】:

  • frame.contentWindow 为空,即使框架不是跨域的
猜你喜欢
  • 2014-05-10
  • 1970-01-01
  • 2019-03-25
  • 2019-09-21
  • 2016-01-11
  • 1970-01-01
  • 2016-03-21
  • 2019-04-09
  • 2020-07-21
相关资源
最近更新 更多