在做項(xiàng)目時(shí),遇到了操作iframe的相關(guān)問(wèn)題。業(yè)務(wù)很簡(jiǎn)單,其實(shí)就是在操作iframe內(nèi)部某個(gè)窗體時(shí),調(diào)用父窗體的一個(gè)函數(shù)。于是就寫(xiě)了兩個(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)行了改寫(xiě),最終代碼如下:
父頁(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ò)改寫(xiě)后,在文件系統(tǒng)中雖然也會(huì)出現(xiàn)那個(gè)錯(cuò)誤,但需要調(diào)用的方法確實(shí)調(diào)用了,目的確實(shí)達(dá)到了,不影響使用了。