js操作iframe兼容各種主流瀏覽器示例代碼
更新時(shí)間:2013年07月22日 17:45:28 作者:
遇到了操作iframe的相關(guān)問(wèn)題,其實(shí)就是在操作iframe內(nèi)部某個(gè)窗體時(shí),調(diào)用父窗體的一個(gè)函數(shù),下面與大家分享下操作iframe兼容各種瀏覽器的方法
在做項(xiàng)目時(shí),遇到了操作iframe的相關(guān)問(wèn)題。業(yè)務(wù)很簡(jiǎn)單,其實(shí)就是在操作iframe內(nèi)部某個(gè)窗體時(shí),調(diào)用父窗體的一個(gè)函數(shù)。于是就寫了兩個(gè)很簡(jiǎn)單的htm頁(yè)面用來(lái)測(cè)試,使用網(wǎng)上流行的方法在谷歌瀏覽器中始終報(bào)錯(cuò),不能通過(guò)。
父頁(yè)面parent.html的代碼如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ParentFunction() {
alert('ParentFunction');
}
</script></head>
<body>
<input type="button" id="btnCancel" class="button" value="測(cè)試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁(yè)面child.html的代碼如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function (e) {
var t=window.parent;
t.ParentFunction();
});
})
</script></head>
<body>
<input type="button" id="btnTest" class="button" value="應(yīng)該獲取的值" />rrr
</body>
</html>
網(wǎng)絡(luò)上流行的方法 var t=window.parent; t.ParentFunction();在IE中能調(diào)用,可是在谷歌瀏覽器中總是提示如下錯(cuò)誤,
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
網(wǎng)上找了很長(zhǎng)時(shí)間都沒(méi)法發(fā)現(xiàn)方法,有的也是很早以前的版本,基本上沒(méi)用了,而且人云亦云,基本上沒(méi)有測(cè)試過(guò)。于是自己摸索,后來(lái)才發(fā)現(xiàn),谷歌瀏覽器其實(shí)那種方法其實(shí)也可以,只是很奇怪,必須發(fā)布后才可以,在文件系統(tǒng)中調(diào)用,就會(huì)出現(xiàn)上邊的錯(cuò)誤。
其實(shí)還有一種html5的方法postMessage,于是就根據(jù)著進(jìn)行了改寫,最終代碼如下:
父頁(yè)面parent.html的代碼如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
this.ParentFunction= function() {//和注釋掉的方法是一樣的,也就是說(shuō)加不加this都是一樣的,因?yàn)榇颂幍膖his就是windows
alert('ParentFunction');
}
// function ParentFunction() {
// alert('ParentFunction');
// }
function receiveMessage(e) {
var data = e.data;
if(data=="ParentFunction")
{
ParentFunction() ;
}
}
if (typeof window.addEventListener != 'undefined') {//使用html5 的postMessage必須處理的
window.addEventListener('message', receiveMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', receiveMessage);
}
</script></head>
<body>
<input type="button" id="btnCancel" class="button" value="測(cè)試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁(yè)面child.html的代碼如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function (e) {
var t=window.parent;
if(!t.ParentFunction)//在不支持時(shí),使用html5 的postMessage方法
{
t.postMessage("ParentFunction", '*');
}
else
{
t.ParentFunction();
}
});
})
</script></head>
<body>
<input type="button" id="btnTest" class="button" value="應(yīng)該獲取的值" />rrr
</body>
</html>
經(jīng)過(guò)改寫后,在文件系統(tǒng)中雖然也會(huì)出現(xiàn)那個(gè)錯(cuò)誤,但需要調(diào)用的方法確實(shí)調(diào)用了,目的確實(shí)達(dá)到了,不影響使用了。
父頁(yè)面parent.html的代碼如下
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ParentFunction() {
alert('ParentFunction');
}
</script></head>
<body>
<input type="button" id="btnCancel" class="button" value="測(cè)試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁(yè)面child.html的代碼如下
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function (e) {
var t=window.parent;
t.ParentFunction();
});
})
</script></head>
<body>
<input type="button" id="btnTest" class="button" value="應(yīng)該獲取的值" />rrr
</body>
</html>
網(wǎng)絡(luò)上流行的方法 var t=window.parent; t.ParentFunction();在IE中能調(diào)用,可是在谷歌瀏覽器中總是提示如下錯(cuò)誤,
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
網(wǎng)上找了很長(zhǎng)時(shí)間都沒(méi)法發(fā)現(xiàn)方法,有的也是很早以前的版本,基本上沒(méi)用了,而且人云亦云,基本上沒(méi)有測(cè)試過(guò)。于是自己摸索,后來(lái)才發(fā)現(xiàn),谷歌瀏覽器其實(shí)那種方法其實(shí)也可以,只是很奇怪,必須發(fā)布后才可以,在文件系統(tǒng)中調(diào)用,就會(huì)出現(xiàn)上邊的錯(cuò)誤。
其實(shí)還有一種html5的方法postMessage,于是就根據(jù)著進(jìn)行了改寫,最終代碼如下:
父頁(yè)面parent.html的代碼如下
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
this.ParentFunction= function() {//和注釋掉的方法是一樣的,也就是說(shuō)加不加this都是一樣的,因?yàn)榇颂幍膖his就是windows
alert('ParentFunction');
}
// function ParentFunction() {
// alert('ParentFunction');
// }
function receiveMessage(e) {
var data = e.data;
if(data=="ParentFunction")
{
ParentFunction() ;
}
}
if (typeof window.addEventListener != 'undefined') {//使用html5 的postMessage必須處理的
window.addEventListener('message', receiveMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', receiveMessage);
}
</script></head>
<body>
<input type="button" id="btnCancel" class="button" value="測(cè)試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁(yè)面child.html的代碼如下
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function (e) {
var t=window.parent;
if(!t.ParentFunction)//在不支持時(shí),使用html5 的postMessage方法
{
t.postMessage("ParentFunction", '*');
}
else
{
t.ParentFunction();
}
});
})
</script></head>
<body>
<input type="button" id="btnTest" class="button" value="應(yīng)該獲取的值" />rrr
</body>
</html>
經(jīng)過(guò)改寫后,在文件系統(tǒng)中雖然也會(huì)出現(xiàn)那個(gè)錯(cuò)誤,但需要調(diào)用的方法確實(shí)調(diào)用了,目的確實(shí)達(dá)到了,不影響使用了。
您可能感興趣的文章:
相關(guān)文章
學(xué)習(xí)JavaScript設(shè)計(jì)模式之裝飾者模式
這篇文章主要為大家介紹了JavaScript設(shè)計(jì)模式中的裝飾者模式,對(duì)JavaScript設(shè)計(jì)模式感興趣的小伙伴們可以參考一下2016-01-01
基于jstree使用AJAX請(qǐng)求獲取數(shù)據(jù)形成樹
這篇文章主要為大家詳細(xì)介紹了基于jstree使用AJAX請(qǐng)求獲取數(shù)據(jù)形成樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
cropper js基于vue的圖片裁剪上傳功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了cropper js基于vue的圖片裁剪上傳功能的相關(guān)資料,需要的朋友可以參考下2018-03-03
HTML5游戲引擎LTweenLite實(shí)現(xiàn)的超帥動(dòng)畫效果(附demo源碼下載)
這篇文章主要介紹了HTML5游戲引擎LTweenLite實(shí)現(xiàn)的超帥動(dòng)畫效果,詳細(xì)分析了LTweenLite的下載,動(dòng)畫效果的實(shí)現(xiàn)步驟,并附帶完整的demo實(shí)例源碼供讀者下載,需要的朋友可以參考下2016-01-01
js實(shí)現(xiàn)拖拽 閉包函數(shù)詳細(xì)介紹
在開發(fā)過(guò)程中可能會(huì)使用js實(shí)現(xiàn)拖拽等相關(guān)功能,本文將以此問(wèn)題進(jìn)行深入介紹,需要了解的朋友可以參考下2012-11-11
TypeScript中的交叉類型和聯(lián)合類型示例講解
交叉類型簡(jiǎn)單來(lái)說(shuō)就是通過(guò)&符號(hào)將多個(gè)類型進(jìn)行合并成一個(gè)類型,然后用type來(lái)聲明新生成的類型,聯(lián)合類型和交叉類型比較相似,聯(lián)合類型通過(guò)|符號(hào)連接多個(gè)類型從而生成新的類型,本文就這兩個(gè)類型結(jié)合示例代碼詳細(xì)講解,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2022-12-12

