關(guān)于Javascript與iframe的那些事兒
同域或跨子域讀寫(xiě)操作 iframe 里的內(nèi)容
父頁(yè)面讀寫(xiě)操作子頁(yè)面:
<iframe id="test-iframe" name="test-iframe" src="child.html" scrolling="no" frameborder="0"></iframe>
<script>
window.onload = function () {
/*
* 下面兩種獲取節(jié)點(diǎn)內(nèi)容的方式都可以。
* 由于 IE6, IE7 不支持 contentDocument 屬性,所以此處用了通用的
* window.frames["iframe Name"] or window.frames[index]
*/
var d = window.frames["test-iframe"].document;
d.getElementsByTagName('h1')[0].innerHTML = 'pp';
alert(d.getElementsByTagName('h1')[0].firstChild.data);
}
</script>
注:在請(qǐng)務(wù)必通過(guò) window.onload 方法訪(fǎng)問(wèn) iframe 中的節(jié)點(diǎn),否則瀏覽器會(huì)提示錯(cuò)誤-拒絕訪(fǎng)問(wèn)。在 IE8, Firefox3.6, Opera11 下在DOMReady 時(shí)也可以訪(fǎng)問(wèn) iframe 中的節(jié)點(diǎn)。
子頁(yè)面讀寫(xiě)操作父頁(yè)面:
<script>
parent.document.getElementsByTagName('h1')[0].innerHTML = 'pp';
alert(parent.document.getElementsByTagName('h1')[0].firstChild.data);
</script>
小結(jié):
•1 測(cè)試通過(guò) IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5.
•2 查閱資料得出 document.getElementById(‘id name').contentDocument 全等于 window.frames["iframe Name"].document.
•3 跨子域時(shí),需要在父頁(yè)面和子頁(yè)面 JS 中分別加上 document.domain = 'xxx.com';
跨域操作 iframe 里內(nèi)容
當(dāng)兩個(gè)網(wǎng)頁(yè)所在域不同時(shí),要實(shí)現(xiàn)數(shù)據(jù)的互相調(diào)用,只能通過(guò) JS 改變 location 對(duì)象的 hash 屬性的值來(lái)做到互相通信。
父頁(yè)面:
<iframe id="test-iframe" src="http://www.yyy.com/child.html" scrolling="no" frameborder="0"></iframe>
<input type="button" value="send" onclick="sendRequest()" />
<script>
function sendRequest() {
document.getElementById('test-iframe').src += '#a';
}
var interval = window.setInterval(function(){
if(location.hash) {
alert(location.hash);
window.clearInterval(interval);
}
},1000);
</script>
子頁(yè)面:
<h1>RRRRRR</h1>
<script>
var url = 'http://www.xxx.com/father.html';
oldHash = self.location.hash,
newHash,
interval = window.setInterval(function(){
newHash = self.location.hash;
if(oldHash != self.location.hash) {
document.getElementsByTagName('h1')[0].innerHTML = 'pp';
//alert(parent.location.href); //去掉這個(gè)注釋?zhuān)瑸g覽器會(huì)提示無(wú)權(quán)限
parent.location.href = url + '#b';
window.clearInterval(interval);
}
},500);
</script>
小結(jié):
•1 經(jīng)測(cè)試 IE6, IE7, IE8, Firefox2.0, Firefox3.0, Firefox3.6, Chrome8, Opera11, Safari5, 除 IE6, IE7 外只要改變hash 就會(huì)記錄在瀏覽器 history 中。
•2 我有試圖在子頁(yè)面用 parent.location.replace 的方法來(lái)避免父頁(yè)面向服務(wù)器發(fā)送請(qǐng)求而進(jìn)行跳轉(zhuǎn),這樣理論上瀏覽器就不會(huì)記錄歷史,但未能奏效。
•2 子頁(yè)面無(wú)權(quán)讀取父頁(yè)面的 url, 但可以對(duì)父頁(yè)面的 url 進(jìn)行寫(xiě)操作,所以跨域操作時(shí),要提前得知父頁(yè)面的url
由于前端解決跨域問(wèn)題的局限性比較大,所以最好用服務(wù)器端解決方案
- 通過(guò)javascript獲取iframe里的值示例代碼
- JavaScript自動(dòng)設(shè)置IFrame高度的小例子
- JavaScript設(shè)置IFrame高度自適應(yīng)(兼容各主流瀏覽器)
- JavaScript 處理Iframe自適應(yīng)高度(同或不同域名下)
- javascript開(kāi)發(fā)隨筆3 開(kāi)發(fā)iframe富文本編輯器的一點(diǎn)體會(huì)
- 兼容IE和Firefox的javascript獲取iframe文檔內(nèi)容的函數(shù)
- javascript+iframe 實(shí)現(xiàn)無(wú)刷新載入整頁(yè)的代碼
- javascript控制frame,iframe的src屬性代碼
- javascript iframe編程相關(guān)代碼
- JavaScript iframe的相互操作淺析
- javascript IFrame 強(qiáng)制刷新代碼
- javascript iframe內(nèi)的函數(shù)調(diào)用實(shí)現(xiàn)方法
- javascript 裝載iframe子頁(yè)面,自適應(yīng)高度
- javascript iframe中打開(kāi)文件,并檢測(cè)iframe存在否
- javascript iFrame研究
- 執(zhí)行iframe中的javascript方法
- javascript之可拖動(dòng)的iframe效果代碼
- javascript 網(wǎng)站常用的iframe分割
相關(guān)文章
JavaScript 動(dòng)態(tài)添加腳本,并觸發(fā)回調(diào)函數(shù)的實(shí)現(xiàn)代碼
JavaScript 動(dòng)態(tài)添加腳本,并觸發(fā)回調(diào)函數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-01-01通過(guò)實(shí)例了解Render Props回調(diào)地獄解決方案
這篇文章主要介紹了通過(guò)實(shí)例了解Render Props回調(diào)地獄解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11javascript中bind函數(shù)的作用實(shí)例介紹
bind()的方法在ie,6,7,8中不適用,需要擴(kuò)展通過(guò)擴(kuò)展Function prototype可以實(shí)現(xiàn)此方法,下面為大家介紹下javascript中bind函數(shù)的作用2014-09-09List Information About the Binary Files Used by an Applicati
List Information About the Binary Files Used by an Application...2007-06-06Javascript查詢(xún)DBpedia小應(yīng)用實(shí)例學(xué)習(xí)
本文則嘗試?yán)肧PARQLWrapper.js來(lái)讀取DBpedia的數(shù)據(jù),并顯示出來(lái),感興趣的你可以參考下,或許對(duì)你有所幫助2013-03-03js實(shí)現(xiàn)全國(guó)省份城市級(jí)聯(lián)下拉菜單效果代碼
這篇文章主要介紹了js實(shí)現(xiàn)全國(guó)省份城市級(jí)聯(lián)下拉菜單效果代碼,通過(guò)JavaScript針對(duì)數(shù)組的定義與元素的遍歷實(shí)現(xiàn)省市級(jí)聯(lián)菜單功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09微信小程序跳轉(zhuǎn)外部鏈接的詳細(xì)實(shí)現(xiàn)方法
寫(xiě)這個(gè)是因?yàn)樽罱〕绦虻囊粋€(gè)需求需要從小程序跳轉(zhuǎn)到客戶(hù)的官網(wǎng),或者其他外部報(bào)名鏈接,下面這篇文章主要給大家介紹了關(guān)于微信小程序跳轉(zhuǎn)外部鏈接的詳細(xì)實(shí)現(xiàn)方法,需要的朋友可以參考下2022-10-10JavaScript數(shù)組排序reverse()和sort()方法詳解
這篇文章主要介紹了JavaScript數(shù)組排序reverse()和sort()方法詳解,需要的朋友可以參考下2017-12-12用js代碼和插件實(shí)現(xiàn)wordpress雪花飄落效果的四種方法
這篇文章主要介紹了用js代碼和插件實(shí)現(xiàn)wordpress雪花飄落效果的四種方法,需要的朋友可以參考下2014-12-12JS.getTextContent(element,preformatted)使用介紹
JS.getTextContent獲取標(biāo)簽的文字想必大家并不陌生吧,下面為大家介紹下具體的使用方法,感興趣的朋友可以參考下2013-09-09