[英]Catch set 'X-Frame-Options' to 'sameorigin error in js
I load some page in iframe.我在 iframe 中加载了一些页面。 I get the error
我收到错误
Refused to display 'url' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
How can I handle this type of error in javascript, and show my message for this case?我如何在 javascript 中处理这种类型的错误,并在这种情况下显示我的消息?
window.onerror
doesn't catch this error. window.onerror
没有捕捉到这个错误。 Is there any possible way to do this?有没有可能的方法来做到这一点?
DOMException comes to help here. DOMException 在这里提供帮助。
I got a trick working as follows: [have not tested cross-browser!]我有一个技巧如下:[尚未测试跨浏览器!]
Define iframe's onload event handler defined as定义 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); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.