检查 iframe 内容是否已经加载
Check if iframe content has been loaded already
我正在尝试检查 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>
您需要在代码中添加一个标志才能再次停止测试运行
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";
}
}
我正在尝试检查 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>
您需要在代码中添加一个标志才能再次停止测试运行
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";
}
}