動(dòng)態(tài)加載js、css等文件跨iframe實(shí)現(xiàn)
更新時(shí)間:2014年02月24日 15:17:00 作者:
這篇文章主要介紹了動(dòng)態(tài)加載js、css等文件跨iframe實(shí)現(xiàn)的方法,需要的朋友可以參考下
1、動(dòng)態(tài)加載js,css文件(用原生js和jquery)
iframe結(jié)構(gòu):
frame0(父)
frame2(子)
frame3(子)
frame2中觸發(fā)事件,動(dòng)態(tài)的向frame3中 加載js、css文件和 dom元素?
*同級(jí)之間可以調(diào)用,可以 通過(guò) 子-父-子 的方式調(diào)用同級(jí)
parent.parentFram(“這個(gè)方法在調(diào)用其他子farme”);
1.jquery的append()
速度快,同步(需要引入jquery)
var oBody = document.getElementById("frame3_id").contentWindow.$("body");
var str = "<div>...</div>"
var scriptTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!scriptTag){
oBody.append(str);
}
var oScript= document.createElement("script");
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var oTag1 = document.getElementById("frame3_id").contentWindow.document.getElementById("oScript1");
if(!oTag1){
oBody.append(oScript);
}
document.getElementById("frame3_id").contentWindow.test(); // 調(diào)用frame3_id 中的test()方法
***********************************
上述例子:a.需要引入jquery;
***********************************
2.js 的appendChild()
速度慢,異步(需要判斷js是否加載完畢)
列子2:
var str = "<div>...</div>"
var popDiv=document.createElement('div');
popDiv.style.xx = xxx;
popDiv.id = "pop";
popDiv.innerHTML = str;
var oBody = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("body")[0];
var oHead = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("head");
if(oHead && oHead.length){
oHead = oHead[0];
}else{
oHead = oBody;
}
var elemDivTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!elemDivTag){
oBody.appendChild(popDiv)
}
var oScript= document.createElement("script"); (css類(lèi)似)
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var scriptTag = document.getElementById("main").contentWindow.document.getElementById("oScript1");
if(!scriptTag){
oHead.appendChild(oScript);
}
oScript.onload = oScript.onreadystatechange = function(){
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
document.getElementById("main").contentWindow.test(); // test()方法 在 引入的js文件中
}else{
console.info("can not load the oScript2.js file");
}
}
iframe結(jié)構(gòu):
frame0(父)
frame2(子)
frame3(子)
frame2中觸發(fā)事件,動(dòng)態(tài)的向frame3中 加載js、css文件和 dom元素?
*同級(jí)之間可以調(diào)用,可以 通過(guò) 子-父-子 的方式調(diào)用同級(jí)
parent.parentFram(“這個(gè)方法在調(diào)用其他子farme”);
1.jquery的append()
復(fù)制代碼 代碼如下:
速度快,同步(需要引入jquery)
var oBody = document.getElementById("frame3_id").contentWindow.$("body");
var str = "<div>...</div>"
var scriptTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!scriptTag){
oBody.append(str);
}
var oScript= document.createElement("script");
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var oTag1 = document.getElementById("frame3_id").contentWindow.document.getElementById("oScript1");
if(!oTag1){
oBody.append(oScript);
}
document.getElementById("frame3_id").contentWindow.test(); // 調(diào)用frame3_id 中的test()方法
***********************************
上述例子:a.需要引入jquery;
***********************************
2.js 的appendChild()
速度慢,異步(需要判斷js是否加載完畢)
列子2:
復(fù)制代碼 代碼如下:
var str = "<div>...</div>"
var popDiv=document.createElement('div');
popDiv.style.xx = xxx;
popDiv.id = "pop";
popDiv.innerHTML = str;
var oBody = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("body")[0];
var oHead = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("head");
if(oHead && oHead.length){
oHead = oHead[0];
}else{
oHead = oBody;
}
var elemDivTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!elemDivTag){
oBody.appendChild(popDiv)
}
var oScript= document.createElement("script"); (css類(lèi)似)
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var scriptTag = document.getElementById("main").contentWindow.document.getElementById("oScript1");
if(!scriptTag){
oHead.appendChild(oScript);
}
oScript.onload = oScript.onreadystatechange = function(){
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
document.getElementById("main").contentWindow.test(); // test()方法 在 引入的js文件中
}else{
console.info("can not load the oScript2.js file");
}
}
您可能感興趣的文章:
- js檢測(cè)iframe是否加載完成的方法
- js下判斷 iframe 是否加載完成的完美方法
- JS加載iFrame出現(xiàn)空白問(wèn)題的解決辦法
- 基于JS判斷iframe是否加載成功的方法(多種瀏覽器)
- JS iFrame加載慢怎么解決
- js通過(guò)iframe加載外部網(wǎng)頁(yè)的實(shí)現(xiàn)代碼
- javascript實(shí)現(xiàn)iframe框架延時(shí)加載的方法
- javascript firefox 自動(dòng)加載iframe 自動(dòng)調(diào)整高寬示例
- js中頁(yè)面的重新加載(當(dāng)前頁(yè)面/上級(jí)頁(yè)面)及frame或iframe元素引用介紹
- javascript應(yīng)用:Iframe自適應(yīng)其加載的內(nèi)容高度
- JS判斷iframe是否加載完成的方法
相關(guān)文章
JavaScript圣杯布局與雙飛翼布局實(shí)現(xiàn)案例詳解
這篇文章主要介紹了JavaScript圣杯布局與雙飛翼布局實(shí)現(xiàn)案例,這是前端面試中需要掌握的知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
基于Bootstrap框架實(shí)現(xiàn)圖片切換
這篇文章主要介紹了基于Bootstrap框架實(shí)現(xiàn)圖片切換的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
require導(dǎo)入module.exports 或 exports導(dǎo)出的使用方法
module.exports用于導(dǎo)出整個(gè)模塊的內(nèi)容,可以通過(guò)賦值給 module.exports 導(dǎo)出一個(gè)對(duì)象、函數(shù)或值,導(dǎo)出的內(nèi)容可以被其他模塊通過(guò)require 導(dǎo)入,本文給大家介紹require導(dǎo)入module.exports 或 exports導(dǎo)出的使用,感興趣的朋友一起看看吧2023-11-11
JS實(shí)現(xiàn)仿百度文庫(kù)評(píng)分功能
本文給大家分享基于js實(shí)現(xiàn)仿百度文庫(kù)評(píng)分功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
javascript獲取文檔坐標(biāo)和視口坐標(biāo)
制作網(wǎng)頁(yè)的過(guò)程中,你有時(shí)候需要知道某個(gè)元素在網(wǎng)頁(yè)上的確切位置。下面的教程總結(jié)了Javascript在網(wǎng)頁(yè)定位方面的相關(guān)知識(shí)。有需要的小伙伴可以參考下。2015-05-05
javascript Deferred和遞歸次數(shù)限制實(shí)例
你知道Deferred和遞歸次數(shù)限制嗎?如果還不知道,可以看看下面的實(shí)例,很好,適合新手朋友們2014-10-10

