有没有什么好方法可以检测由于X-Frame-Options标题,页面何时不会显示在页面中?我知道我可以请求页面服务器端并查找标题,但我很好奇浏览器是否有任何机制来捕获此错误。
答案 0 :(得分:12)
好的,这个很旧但仍然相关。
<强>事实:强> 当iframe加载被X-Frame-Options阻止的网址时,加载时间非常短。
<强>哈克:强> 因此,如果立即发生onload,我知道 可能是 X-Frame-Options问题。
<强>声明:强> 这可能是我写过的“最讨厌的”代码之一,所以不要指望太多:
var timepast=false;
var iframe = document.createElement("iframe");
iframe.style.cssText = "position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;";
iframe.src = "http://pix.do"; // This will work
//iframe.src = "http://google.com"; // This won't work
iframe.id = "theFrame";
// If more then 500ms past that means a page is loading inside the iFrame
setTimeout(function() {
timepast = true;
},500);
if (iframe.attachEvent){
iframe.attachEvent("onload", function(){
if(timepast) {
console.log("It's PROBABLY OK");
}
else {
console.log("It's PROBABLY NOT OK");
}
});
}
else {
iframe.onload = function(){
if(timepast) {
console.log("It's PROBABLY OK");
}
else {
console.log("It's PROBABLY NOT OK");
}
};
}
document.body.appendChild(iframe);
答案 1 :(得分:7)
免责声明:我在2012年写的这个答案(Chrome当时的版本是~20)已经过时了,我会将它保留在这里仅用于历史目的。阅读和使用需要您自担风险。
好的,这是一个有点老问题,但这是我发现的(这不是一个完整的答案)Chrome / Chromium。
检测指向外部地址的帧是否已加载的方式只是尝试访问其contentWindow或文档。
这是我使用的代码:
element.innerHTML = '<iframe class="innerPopupIframe" width="100%" height="100%" src="'+href+'"></iframe>';
myframe = $(element).find('iframe');
然后,后来:
try {
var letstrythis = myframe.contentWindow;
} catch(ex) {
alert('the frame has surely started loading');
}
事实是,如果X-Frame-Options禁止访问,则可以访问myFrame.contentWindow
。
这里的问题就是我所说的“当时,后来”。我还没有想出要依赖什么,哪个事件可以用来查找何时是进行测试的好时机。
答案 2 :(得分:0)
我唯一能想到的是代理网址的AJAX请求,然后查看标题,如果它没有X-Frame-Options,则在iframe中显示它。远非理想,但总比没有好。
答案 3 :(得分:0)
至少在Chrome中,您可以注意到加载失败,因为iframe.onload事件未触发。您可以将其用作指示页面可能不允许iframing。
答案 4 :(得分:0)
这是基于@Iftach的答案,但稍微不那么苛刻。
检查iframe.contentWindow.length > 0
是否表明iframe已成功加载。
此外,它会检查iframe onload
事件是否在5秒内触发,并发出警告。这可以捕获混合内容的加载失败(虽然是一种黑客的方式)。
var iframeLoaded = false;
var iframe = document.createElement('iframe');
// ***** SWAP THE `iframe.src` VALUE BELOW FOR DIFFERENT RESULTS ***** //
// iframe.src = "https://davidsimpson.me"; // This will work
iframe.src = "https://google.com"; // This won't work
iframe.id = 'theFrame';
iframe.style.cssText = 'position:fixed; top:40px; left:10px; bottom:10px;'
+ 'right:10px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;';
var iframeOnloadEvent = function () {
iframeLoaded = true;
var consoleDiv = document.getElementById('console');
if (iframe.contentWindow.length > 0) {
consoleDiv.innerHTML = '✔ Content window loaded: ' + iframe.src;
consoleDiv.style.cssText = 'color: green;'
} else {
consoleDiv.innerHTML = '✘ Content window failed to load: ' + iframe.src;
consoleDiv.style.cssText = 'color: red;'
}
}
if (iframe.attachEvent){
iframe.attachEvent('onload', iframeOnloadEvent);
} else {
iframe.onload = iframeOnloadEvent;
}
document.body.appendChild(iframe);
// iframe.onload event doesn't trigger in firefox if loading mixed content (http iframe in https parent) and it is blocked.
setTimeout(function () {
if (iframeLoaded === false) {
console.error('%c✘ iframe failed to load within 5s', 'font-size: 2em;');
consoleDiv.innerHTML = '✘ iframe failed to load within 5s: ' + iframe.src;
consoleDiv.style.cssText = 'color: red;'
}
}, 5000);
现场演示 - https://jsfiddle.net/dvdsmpsn/7qusz4q3/ - 您可以在相关浏览器中对其进行测试。
在撰写本文时,它适用于Chrome,Safari,Opera,Firefox,Vivaldi等上的当前版本。 Internet Explorer 11.我没有在其他浏览器中测试它。
答案 5 :(得分:0)
在线测试工具可能很有用。 我用了https://www.hurl.it/。 你可以清楚地看到响应头。 寻找X-frame-option。如果值为拒绝 - 它将不会显示在iframe中。 相同的起源 - 仅来自同一个域, 允许 - 允许来自特定网站。
如果您想尝试其他工具,可以直接在Google上进行&#39; http请求测试。
答案 6 :(得分:-1)
这可以通过
来实现a)通过CreateElement
创建一个新的IFrameb)将其显示设置为&#39;无&#39;
c)通过src属性
加载URLd)为了等待iframe加载,使用SetTimeOut方法延迟函数调用(我将调用延迟了10秒)
e)在该功能中,检查ContentWindow长度。
以下是示例代码:
function isLoaded(val) {
var elemId = document.getElementById('ctlx');
if (elemId != null)
document.body.removeChild(elemId);
var obj= document.createElement('iframe');
obj.setAttribute("id", "ctlx");
obj.src = val;
obj.style.display = 'none';
document.body.appendChild(obj);
setTimeout(canLoad, 10000);
}
function canLoad() {
//var elemId = document.getElementById('ctl100');
var elemId = document.getElementById('ctlx');
if (elemId.contentWindow.length > 0) {
elemId.style.display = 'inline';
}
else {
elemId.src = '';
elemId.style.display = 'none';
alert('not supported');
}
}
答案 7 :(得分:-1)
这就是我根据我的要求检查X-Frames-Options的方法。在加载JSP页面时,您可以使用AJAX向特定URL发送异步请求,如下所示:
var request = new XMLHttpRequest();
request.open('GET', <insert_URL_here>, false);
request.send(null);
完成此操作后,您可以按如下方式阅读收到的响应标头:
var headers = request.getAllResponseHeaders();
然后,您可以对此进行迭代,以找出X-Frames-Options的值。获得该值后,您可以在适当的逻辑中使用它。