Javascript 不同的多个iFrame,跨多个页面?

Javascript 不同的多个iFrame,跨多个页面?,javascript,iframe,Javascript,Iframe,我的代码在id“greMapTempe”后停止正常运行;我想这是因为它在插入的许多页面中找不到该ID。我加载的iFrame在每个页面上都不同,因此我需要脚本跳过它找不到的ID,然后继续查找下一个ID。我可以对blow代码做什么来确保检查所有ID,即使它们不在页面中 函数loadDeferedFrame(){ var iframe=document.getElementById(“greYoutube”); iframe.src=”https://www.youtube.com/embed/h7

我的代码在id“greMapTempe”后停止正常运行;我想这是因为它在插入的许多页面中找不到该ID。我加载的iFrame在每个页面上都不同,因此我需要脚本跳过它找不到的ID,然后继续查找下一个ID。我可以对blow代码做什么来确保检查所有ID,即使它们不在页面中

函数loadDeferedFrame(){
var iframe=document.getElementById(“greYoutube”);
iframe.src=”https://www.youtube.com/embed/h7_w43jJ0Ts"
var iframe=document.getElementById(“greMapTempe”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3330.0372197439724!2d-111.96932568528953!3d33.4222739802116!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2ZMZPCSDI1JZIWLJIITAXMTHU4JZAXLIVW!5e0!3m2!1sen!2sus!4V14867741925“
var iframe=document.getElementById(“gremapcinnati”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3086.8059851441258!2d-84.4652936843564!3d39.31532112980541!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!10x88404FB4EC94A687%30x915A9A0DB6E53917!2大+房间+逃生+辛辛那提!5e0!3m2!3m2!1sen!2sus!4V1546663243“
var iframe=document.getElementById(“greMapCleveland”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2992.564362618732!2d-81.627932484311!3d41.405266202849035!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!10x8830E442A6185089%30x90787A2298A6C3C2!2大+房间+逃生+克利夫兰!5e0!3m2!1sen!2sus!4V154144650618“
var iframe=document.getElementById(“greMapColumbus”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12214.714268930691!2d-82.9792519!3d40.0602736!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3OXD1A0EE0AA5FB171A!2大+房间+逃生+哥伦布!5e0!3m2!1sen!2sus!4V15414463773“
var iframe=document.getElementById(“greMapAustin”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3443.8281848309443!2d-97.71271668471364!3d30.32740461210432!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8644ca3ced1c17f7%3A0X7BD80180添加156D1!2sMind+Spark!5e0!3m2!1sen!2sus!4v1472767173729“
var iframe=document.getElementById(“gremapdalas”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d13390.834398025358!2d-96.7070997!3d32.9587021!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0897C1A032F856003!2大+房间+逃生+达拉斯!5e0!3m2!1sen!2sus!4V1546025561“
var iframe=document.getElementById(“greSanAntonio”);
iframe.src=”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d13900.967422077894!2d-98.4774668!3d29.4217269!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xfee289ea3851e800!2大+房间+逃生+圣安东尼奥!5e0!3m2!1sen!2sus!4V1545639522“
};

window.onload=loadDeferedFrame在设置src之前检查元素是否存在,否则您将遇到错误,函数的执行将停止

函数loadDeferedFrame(){
var tryLoadIFrame=函数(ID、URL){
var iFrame=document.getElementById(ID);
if(iFrame){
iFrame.src=URL;
}
}
tryLoadIFrame(“greYoutube”https://www.youtube.com/embed/h7_w43jJ0Ts");
tryLoadIFrame(“greMapTempe”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3330.0372197439724!2d-111.96932568528953!3d33.4222739802116!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zmzzpcsdi1jziwljiinitiaxmcsdu4jzaxlivw!5e0!3m2!1sen!2sus!4v148741925”);
tryLoadIFrame(“Gremapcinnati”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3086.8059851441258!2d-84.4652936843564!3d39.31532112980541!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!10x88404FB4EC94A687%30x915A9A0DB6E53917!2大+房间+逃生+辛辛那提!5e0!3m2!1S2!1sen!2sus!4V1546663243”);
tryLoadIFrame(“greMapCleveland”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2992.564362618732!2d-81.627932484311!3d41.405266202849035!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8830e442a6185089%3A0x90787a2298a6c3c2!2大+房间+逃生+克利夫兰!5e0!3m2!1sen!2sus!4V154144650618”);
TRYLADIFRAME(“greMapColumbus”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d12214.714268930691!2d-82.9792519!3d40.0602736!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xd1a0ee0aa5fb171a!2大+房间+逃生+哥伦布!5e0!3m2!1sen!2sus!4v1541446346773”);
TRYLADIFRAME(“Gremapoustin”https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3443.8281848309443!2d-97.71271668471364!3d30.32740461210432!2m3!1f0!2f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8644ca3ced1c17f7%3A0X7BD80180180AD156D1!2sMind+Spark!5e0!3m2!1sen!2sus!4v1472767173729”);
TRYLADIFRAME(“Gremapdalas”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d13390.834398025358!2d-96.7070997!3d32.9587021!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0897C1A032F856003!2大+房间+逃生+达拉斯!5e0!3m2!1sen!2sus!4V1546025561”);
tryLoadIFrame(“格雷桑安东尼奥”https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d13900.967422077894!2d-98.4774668!3d29.4217269!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xfee289ea3851e800!2大+房间+逃生+圣安东尼奥!5e0!3m2!1sen!2sus!4V1545639522”);
};

window.onload=loadDeferedFrame有多种解决方法:

1-在更改变量的
src
attr之前,检查变量是否不为空: 最基本的方法,但可能是最冗余的(除非您使用像@user1538301的答案这样的帮助函数):

2-使用jQuery: 使用jQuery按ID(或任何选择器)修改元素不会抛出错误,即使没有匹配项

//will always work, even if there is no match
$("#greYoutube").attr("src", "https://www.youtube.com/embed/h7_w43jJ0Ts");
3-使用ID以外的选择器: 第三种方法的优点是最简洁、最易于维护,因为不需要像iFrame那样多次复制/粘贴

您还可以为要延迟的iframe设置特定的类名
//will always work, even if there is no match
$("#greYoutube").attr("src", "https://www.youtube.com/embed/h7_w43jJ0Ts");
<iframe class='defer' src='someSrc' data-src='https://www.youtube.com/embed/h7_w43jJ0Ts'/>
//this will not throw an error, because if there is no such element, 
//'iframesToDefer' will be empty and thus we won't enter the loop.
var iframesToDefer = document.getElementsByClassName('defer');
for(var iframe of iframesToDefer) {
    iframe.src = iframe.dataset['src'];
}