隐藏其内容被代理阻止的iframe [英] Hide the iframes whose content get blocked by proxy
问题描述
我有一个包含iframe的网页。
这些iframe用于显示一些外部网站数据。
I have a webpage with iframes. These iframes are for showing some external website data.
但是当这些外部服务器在网络中被阻止时会出现问题,它会出现代理服务器错误是拒绝连接。
这对我来说不太好看。
But problem arise when those external servers get blocked in a network it gives a error that "The proxy server is refusing connections". It does not look good to me.
我想要隐藏所有这些被阻止的iframe,或者想在那里显示一些备用数据。
I want to hide all these blocked iframes or want to show some alternate data there.
推荐答案
无法检查某个网页是否已加载 。但是,可以使用 onload
事件处理程序。
It's not possible to check whether a page has not loaded. However, it's possible to use onload
event handlers.
不依赖于JQuery很重要,因为JQuery也是一个必须加载的外部源。在< script>
标记之后添加此代码最后一个IFRAME元素(通常在正文末尾)。代码:
It's important to not rely on JQuery, because JQuery is also an external source which has to be loaded. Add this code within <script>
tags after the last IFRAME element (often, at the end of the body). Code:
//Cannot rely on JQuery, as it has to be loaded
(function(){//Anonymous wrapper.
var iframes = document.getElementsByTagName("iframe");
var num_frames = iframes.length;
//Function to add Event handlers
var addLoad = window.addEventListener ? function(elem, func){
elem.addEventListener("load", func, true);
} : window.attachEvent ? function(elem, func){
elem.attachEvent("onload", func);
} : function(elem, func){
elem.onload = func;
};
var success_load = 0;
for(var i=0; i<num_frames; i++){
addLoad(iframes[i], function(){
this.dataSuccessfullyLoaded = true;
success_load++;
});
}
addLoad(window, function(){
if(success_load < num_frames){
for(var i=num_frames-1; i>=0; i--){
if(!iframes[i].dataSuccessfullyLoaded){
iframes[i].parentNode.removeChild(iframes[i]);
//Or: iframes[i].style.display = "none";
}
}
}
});
})();
小提琴: http://jsfiddle.net/3vnrg/
编辑
您的代理似乎发送状态代码为200的HTTP页面。另一种选择是包含CSS文件,并检查是否存在CSS变量:
EDIT
Your proxy seems to send HTTP pages with status code 200. Another option is to include the CSS file, and check whether a CSS variable exists or not:
/*From http://static.ak.fbcdn.net/rsrc.php/v1/yb/r/CeiiYqUQjle.css*/
#facebook .hidden_elem{display:none !important}
#facebook .invisible_elem{visibility:hidden}
HTML:
<link rel="Stylesheet" href="http://static.ak.fbcdn.net/rsrc.php/v1/yb/r/CeiiYqUQjle.css" />
<div id="facebook"><div class="hidden_elem invisible_elem"></div></div>
JavaScript(在加载所有资源后执行此代码):
JavaScript (execute this code after all resources have been loaded):
if($("#facebook div").css("display") != "none" || $("#facebook div").css("visibility") != "hidden") disableFBFrame();
// Where disableFBFrame(); is a function which hides the frame.
这篇关于隐藏其内容被代理阻止的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!