本文主要給大家介紹了關(guān)于利用JS對(duì)iframe父子(內(nèi)外)頁面進(jìn)行操作的方法,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
一、獲取iframe里的內(nèi)容
在開始之前,首先我們來看看如何獲取iframe里的內(nèi)容,獲取iframe中內(nèi)容主要的兩個(gè)API就是contentWindow
,和contentDocument iframe.contentWindow
, 獲取iframe的window對(duì)象 iframe.contentDocument
, 獲取iframe的document對(duì)象 這兩個(gè)API只是DOM節(jié)點(diǎn)提供的方式(即getELement系列對(duì)象)
var iframe = document.getElementById("iframe1"); var iwindow = iframe.contentWindow; var idoc = iwindow.document; console.log("window",iwindow);//獲取iframe的window對(duì)象 console.log("document",idoc); //獲取iframe的document console.log("html",idoc.documentElement);//獲取iframe的html console.log("head",idoc.head); //獲取head console.log("body",idoc.body); //獲取body
實(shí)際情況如:
另外更簡單的方式是,結(jié)合Name屬性,通過window提供的frames獲取.
<iframe src ="/index.html" id="ifr1" name="ifr1" scrolling="yes"> <p>Your browser does not support iframes.</p></iframe><script type="text/javascript"> console.log(window.frames['ifr1'].window);console.dir(document.getElementById("ifr1").contentWindow);</script>
其實(shí)window.frames[‘ifr1']
返回的就是window對(duì)象,即
window.frames['ifr1']===window
這里就看你想用哪一種方式獲取window對(duì)象,兩者都行,不過本人更傾向于第二種使用frames[xxx].因?yàn)椋帜干侔∥箏 然后,你就可以操控iframe里面的DOM內(nèi)容。
二、在iframe中獲取父級(jí)內(nèi)容
同理,在同域下,父頁面可以獲取子iframe的內(nèi)容,那么子iframe同樣也能操作父頁面內(nèi)容。在iframe中,可以通過在window上掛載的幾個(gè)API進(jìn)行獲取.
window.parent
獲取上一級(jí)的window對(duì)象,如果還是iframe則是該iframe的window對(duì)象window.top
獲取最頂級(jí)容器的window對(duì)象,即,就是你打開頁面的文檔window.self
返回自身window的引用。可以理解 window===window.self
(腦殘)如圖:
獲取了之后,我們就可以進(jìn)行相關(guān)操作了。 在同域的iframe中,我們可以巧妙的使用iframe的黑科技來實(shí)現(xiàn)一些trick.
iframe的輪詢
話說在很久很久以前,我們實(shí)現(xiàn)異步發(fā)送請(qǐng)求是使用iframe實(shí)現(xiàn)的~! 怎么可能!!! 真的史料為證(自行g(shù)oogle), 那時(shí)候?yàn)榱瞬惶D(zhuǎn)頁面,提交表單時(shí)是使用iframe提交的。現(xiàn)在,前端發(fā)展尼瑪真快,websocket,SSE,ajax等,逆天skill的出現(xiàn),顛覆了iframe, 現(xiàn)在基本上只能活在IE8,9的瀏覽器內(nèi)了。 但是,寶寶以為這樣就可以不用了解iframe了,而現(xiàn)實(shí)就是這么殘酷,我們目前還需要兼容IE8+。所以,iframe 實(shí)現(xiàn)長輪詢和長連接的trick 我們還是需要涉獵滴。
iframe長輪詢
如果寫過ajax的童鞋,應(yīng)該知道,長輪詢就是在ajax的readyState = 4的時(shí),再次執(zhí)行原函數(shù)即可。 這里使用iframe也是一樣,異步創(chuàng)建iframe,然后reload, 和后臺(tái)協(xié)商好, 看后臺(tái)哥哥們將返回的信息放在,然后獲取里面信息即可. 這里是直接放在body里.
var iframeCon = docuemnt.querySelector('#container'), text; //傳遞的信息 var iframe = document.createElement('iframe'), iframe.id = "frame", iframe.style = "display:none;", iframe.name="polling", iframe.src="target.html"; iframeCon.appendChild(iframe); iframe.onload= function(){ var iloc = iframe.contentWindow.location, idoc = iframe.contentDocument; setTimeout(function(){ text = idoc.getElementsByTagName('body')[0].textContent; console.log(text); iloca.reload(); //刷新頁面,再次獲取信息,并且會(huì)觸發(fā)onload函數(shù) },2000); }
這樣就可以實(shí)現(xiàn)ajax的長輪詢的效果。 當(dāng)然,這里只是使用reload進(jìn)行獲取,你也可以添加iframe和刪除iframe的方式,進(jìn)行發(fā)送信息,這些都是根據(jù)具體場景應(yīng)用的。另外在iframe中還可以實(shí)現(xiàn)異步加載js文件,不過,iframe和主頁是共享連接池的,所以還是很蛋疼的,現(xiàn)在基本上都被XHR和hard calllback取締了,這里也不過多介紹了。
1.js在iframe子頁面操作父頁面元素代碼:
window.parent.document.getElementByIdx_x("父頁面元素id");
2.js在父頁面獲取iframe子頁面元素代碼如下:
window.frames["iframe_ID"].document.getElementByIdx_x("子頁面元素id");
3. jquery在iframe子頁面獲取父頁面元素代碼如下:
$("#objid",parent.document)
4. jquery在父頁面獲取iframe子頁面的元素
$("#objid",document.frames('iframename').document)
5.在iframe中調(diào)用父頁面中定義的方法和變量:
window.parent.window.parentMethod();window.parent.window.parentValue;
6.在父頁面操作iframe子頁面的方法和變量
window.frames["iframe_ID"].window.childMethod();window.frames["iframe_ID"].window.childValue;
一、同域下父子頁面的通信
父頁面parent.html
<html><head> <script type="text/javascript"> function say(){ alert("parent.html"); } function callChild(){ myFrame.window.say(); myFrame.window.document.getElementById("button").value="調(diào)用結(jié)束"; } </script></head><body> <input id="button" type="button" value="調(diào)用child.html中的函數(shù)say()" onclick="callChild()"/> <iframe name="myFrame" src="http://caibaojian.com/child.html"></iframe></body></html>
子頁面child.html
<html><head> <script type="text/javascript"> function say(){ alert("child.html"); } function callParent(){ parent.say(); parent.window.document.getElementById("button").value="調(diào)用結(jié)束"; } </script></head><body> <input id="button" type="button" value="調(diào)用parent.html中的say()函數(shù)" onclick="callParent()"/></body></html>
注意事項(xiàng)
要確保在iframe加載完成后再進(jìn)行操作,如果iframe還未加載完成就開始調(diào)用里面的方法或變量,會(huì)產(chǎn)生錯(cuò)誤。判斷iframe是否加載完成有兩種方法:
1. iframe上用onload事件
2. 用document.readyState=="complete"
來判斷
二、跨域父子頁面通信方法
如果iframe所鏈接的是外部頁面,因?yàn)榘踩珯C(jī)制就不能使用同域名下的通信方式了。
1.父頁面向子頁面?zhèn)鬟f數(shù)據(jù)
實(shí)現(xiàn)的技巧是利用location對(duì)象的hash值,通過它傳遞通信數(shù)據(jù)。在父頁面設(shè)置iframe的src后面多加個(gè)data字符串,然后在子頁面中通過某種方式能即時(shí)的獲取到這兒的data就可以了,例如:
1.1 在子頁面中通過setInterval方法設(shè)置定時(shí)器,監(jiān)聽location.href
的變化即可獲得上面的data信息
1.2. 然后子頁面根據(jù)這個(gè)data信息進(jìn)行相應(yīng)的邏輯處理
2.子頁面向父頁面?zhèn)鬟f數(shù)據(jù)
實(shí)現(xiàn)技巧就是利用一個(gè)代理iframe,它嵌入到子頁面中,并且和父頁面必須保持是同域,然后通過它充分利用上面第一種通信方式的實(shí)現(xiàn)原理就把子頁面的數(shù)據(jù)傳遞給代理iframe,然后由于代理的iframe和主頁面是同域的,所以主頁面就可以利用同域的方式獲取到這些數(shù)據(jù)。使用 window.top
或者window.parent.parent
獲取瀏覽器最頂層window對(duì)象的引用。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注