【问题标题】:Check if iframe content has been loaded already检查 iframe 内容是否已经加载
【发布时间】:2021-08-08 16:10:04
【问题描述】:

我正在尝试检查 iframe src 是否已在第一次添加,以便它不会在第二次再次加载它。 iframe 是使用下面给出的函数激活的,我正在使用 jQuery 来检查这一点。不幸的是,每次触发函数时都会调用console.log("loaded");。我做错了什么?


    <iframe id="iframe_01" style="
   position: fixed;
   margin: auto;
   height: 100%;
   width: 100%;
   overflow: hidden;
   z-index: 10;
   display: none;
   ">
    </iframe>

<script>
function Activate_iFrame() {

    //iframe start
    let iframe_01 = document.getElementById("iframe_01");

    iframe_01.style.display = 'Block';

    if ($("#iframe_01").find("iframe").contents().find("body")) {
        console.log("loaded");
        iframe_01.src = "LINK TO WEBSITE";
    }
}
</script>

【问题讨论】:

  • I am trying to check if the iframe src has been already added the first time so that it does not load it again in the second time. 这与问题中的代码完全相反。问题中的代码是确定iframe 的内容中是否已经存在body,然后将内容完全更改为另一个URL。您能否将问题编辑得更清楚。

标签: javascript html jquery iframe


【解决方案1】:

您需要在代码中添加一个标志以停止第二次运行测试

let iframeLoaded = false;

function Activate_iFrame() {

    //iframe start
    let iframe_01 = document.getElementById("iframe_01");

    iframe_01.style.display = 'Block';

    if (!iframeLoaded &&$("#iframe_01").find("iframe").contents().find("body")) {
        console.log("loaded");
        iframeLoaded = true;
        iframe_01.src = "LINK TO WEBSITE";
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2013-04-16
    • 2012-01-07
    • 2013-02-22
    • 2011-05-31
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多