動態(tài)加載js、css等文件跨iframe實現(xiàn)
更新時間:2014年02月24日 15:17:00 作者:
這篇文章主要介紹了動態(tài)加載js、css等文件跨iframe實現(xiàn)的方法,需要的朋友可以參考下
1、動態(tài)加載js,css文件(用原生js和jquery)
iframe結構:
frame0(父)
frame2(子)
frame3(子)
frame2中觸發(fā)事件,動態(tài)的向frame3中 加載js、css文件和 dom元素?
*同級之間可以調用,可以 通過 子-父-子 的方式調用同級
parent.parentFram(“這個方法在調用其他子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(); // 調用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類似)
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結構:
frame0(父)
frame2(子)
frame3(子)
frame2中觸發(fā)事件,動態(tài)的向frame3中 加載js、css文件和 dom元素?
*同級之間可以調用,可以 通過 子-父-子 的方式調用同級
parent.parentFram(“這個方法在調用其他子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(); // 調用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類似)
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檢測iframe是否加載完成的方法
- js下判斷 iframe 是否加載完成的完美方法
- JS加載iFrame出現(xiàn)空白問題的解決辦法
- 基于JS判斷iframe是否加載成功的方法(多種瀏覽器)
- JS iFrame加載慢怎么解決
- js通過iframe加載外部網(wǎng)頁的實現(xiàn)代碼
- javascript實現(xiàn)iframe框架延時加載的方法
- javascript firefox 自動加載iframe 自動調整高寬示例
- js中頁面的重新加載(當前頁面/上級頁面)及frame或iframe元素引用介紹
- javascript應用:Iframe自適應其加載的內容高度
- JS判斷iframe是否加載完成的方法
相關文章
JS實現(xiàn)深度優(yōu)先搜索求解兩點間最短路徑
這篇文章主要為大家詳細介紹了JS實現(xiàn)深度優(yōu)先搜索求解兩點間最短路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01基于javascript實現(xiàn)listbox左右移動
這篇文章主要介紹了基于javascript實現(xiàn)listbox左右移動的相關資料,以一個完整的實例代碼分析了js實現(xiàn)listbox左右移動的相關技巧,感興趣的小伙伴們可以參考一下2016-01-01