gpt4 book ai didi

javascript - 获取跨域 iframe 的响应状态码?

转载 作者:行者123 更新时间:2023-12-04 02:27:43 26 4
gpt4 key购买 nike

我的网站上有一个 iframe,它指向 3rd 方页面(即不在我的域中,我对他们的服务器没有任何控制权)。

我希望能够检查他们的网站是否在 iframe 中正确加载。可能有这样的情况——

  • 它被某些防火墙阻止
  • 他们的服务出了问题什么的。

  • 这样我就可以在这种情况下在 iframe 中显示正确的错误消息。我希望我能以某种方式找出 iframe 的响应状态代码。我怎样才能实现这样的目标?

    最佳答案

    尝试这个。

    <script>
    function checkIframeLoaded() {
    // Get a handle to the iframe element
    iframe = document.getElementById('your_iframe');

    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

    // Check if loading is complete
    if ( iframeDoc.readyState == 'complete' ) {

    iframe.contentWindow.onload = function(){
    alert("I am loaded");
    };

    // The loading is complete, call the function we want executed once the iframe is loaded
    afterLoading();
    return;
    }

    // If we are here, it is not loaded. Set things up so we check the status again in 100 milliseconds
    window.setTimeout('checkIframeLoaded();', 100);
    }

    function afterLoading(){
    alert("I am here");
    }
    </script>

    <body onload="checkIframeLoaded();">

    关于javascript - 获取跨域 iframe 的响应状态码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30191939/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com