隐藏内容被代理阻止的 iframe

发布于 2024-12-05 23:07:47 字数 158 浏览 0 评论 0原文

我有一个带有 iframe 的网页。 这些 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.

I want to hide all these blocked iframes or want to show some alternate data there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-12-12 23:07:47

无法检查页面是否加载。但是,可以使用 onload 事件处理程序。

重要的是不要依赖 JQuery,因为 JQuery 也是必须加载的外部源。将此代码添加到最后一个 IFRAME 元素之后的

//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 变量是否存在:

/*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(在加载所有资源后执行此代码):

if($("#facebook div").css("display") != "none" || $("#facebook div").css("visibility") != "hidden") disableFBFrame();
// Where disableFBFrame(); is a function which hides the frame.

It's not possible to check whether a page has not loaded. However, it's possible to use onload event handlers.

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";
                 }
             }
         }
    });
})();

Fiddle: http://jsfiddle.net/3vnrg/

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 (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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文