简体   繁体   English

检查 iframe 内容是否已经加载

[英]Check if iframe content has been loaded already

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 src,以便它不会在第二次再次加载它。 The iframe is activated with the function given below, I am using jQuery to check this. iframe 使用下面给出的 function 激活,我正在使用 jQuery 进行检查。 Unfortunately the console.log("loaded");不幸的是console.log("loaded"); gets called every time the function is triggered.每次触发 function 时都会被调用。 What am I doing wrong?我究竟做错了什么?


    <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>

You need to add a flag to your code to stop the test running a second time您需要在代码中添加一个标志以停止第二次运行测试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM