js操作iframe兼容各種主流瀏覽器示例代碼
更新時間:2013年07月22日 17:45:28 作者:
遇到了操作iframe的相關(guān)問題,其實就是在操作iframe內(nèi)部某個窗體時,調(diào)用父窗體的一個函數(shù),下面與大家分享下操作iframe兼容各種瀏覽器的方法
在做項目時,遇到了操作iframe的相關(guān)問題。業(yè)務(wù)很簡單,其實就是在操作iframe內(nèi)部某個窗體時,調(diào)用父窗體的一個函數(shù)。于是就寫了兩個很簡單的htm頁面用來測試,使用網(wǎng)上流行的方法在谷歌瀏覽器中始終報錯,不能通過。
父頁面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="測試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁面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)用,可是在谷歌瀏覽器中總是提示如下錯誤,
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
網(wǎng)上找了很長時間都沒法發(fā)現(xiàn)方法,有的也是很早以前的版本,基本上沒用了,而且人云亦云,基本上沒有測試過。于是自己摸索,后來才發(fā)現(xiàn),谷歌瀏覽器其實那種方法其實也可以,只是很奇怪,必須發(fā)布后才可以,在文件系統(tǒng)中調(diào)用,就會出現(xiàn)上邊的錯誤。
其實還有一種html5的方法postMessage,于是就根據(jù)著進(jìn)行了改寫,最終代碼如下:
父頁面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() {//和注釋掉的方法是一樣的,也就是說加不加this都是一樣的,因為此處的this就是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="測試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁面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)//在不支持時,使用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)過改寫后,在文件系統(tǒng)中雖然也會出現(xiàn)那個錯誤,但需要調(diào)用的方法確實調(diào)用了,目的確實達(dá)到了,不影響使用了。
父頁面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="測試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁面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)用,可是在谷歌瀏覽器中總是提示如下錯誤,
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
網(wǎng)上找了很長時間都沒法發(fā)現(xiàn)方法,有的也是很早以前的版本,基本上沒用了,而且人云亦云,基本上沒有測試過。于是自己摸索,后來才發(fā)現(xiàn),谷歌瀏覽器其實那種方法其實也可以,只是很奇怪,必須發(fā)布后才可以,在文件系統(tǒng)中調(diào)用,就會出現(xiàn)上邊的錯誤。
其實還有一種html5的方法postMessage,于是就根據(jù)著進(jìn)行了改寫,最終代碼如下:
父頁面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() {//和注釋掉的方法是一樣的,也就是說加不加this都是一樣的,因為此處的this就是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="測試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>
子頁面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)//在不支持時,使用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)過改寫后,在文件系統(tǒng)中雖然也會出現(xiàn)那個錯誤,但需要調(diào)用的方法確實調(diào)用了,目的確實達(dá)到了,不影響使用了。
相關(guān)文章
javascript實現(xiàn)實時輸出當(dāng)前的時間
在網(wǎng)頁中實時的顯示時間,不但可以給網(wǎng)頁添色,還可以方便瀏覽者掌握當(dāng)前時間,為了提高網(wǎng)站的開發(fā)速度,可以把主代碼封裝在一個單獨的函數(shù)里面,在需要的時候直接調(diào)用而我為了演示,直接寫在了主頁面,方便大家觀看2015-04-04使用javascript函數(shù)編寫簡單銀行取錢存錢流程
本文通過實例代碼給大家講解了使用javascript函數(shù)編寫簡單銀行取錢存錢流程,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05Javascript繼承機(jī)制的設(shè)計思想分享
我花了很多時間,學(xué)習(xí)這個部分,還做了很多筆記。但是都屬于強(qiáng)行記憶,無法從根本上理解。2011-08-08innerHTML innerText textContent使用區(qū)別示例詳解
這篇文章主要為大家介紹了innerHTML innerText textContent使用區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11js中常用的4種模糊查詢詳解(includes()、indexOf()、search()、match())
這篇文章主要給大家介紹了關(guān)于js中常用的4種模糊查詢(includes()、indexOf()、search()、match())的相關(guān)資料,搜索可以使我們更快的找到某一個關(guān)鍵詞或者某一個商品,所以模糊查詢和下拉匹配也成了前端必備的一個小技能,需要的朋友可以參考下2023-11-11如何設(shè)置一定時間內(nèi)只能發(fā)送一次請求
這篇文章主要介紹了如何設(shè)置一定時間內(nèi)只能發(fā)送一次請求,需要的朋友可以參考下2014-02-02