js操作iframe相容各種主流瀏覽器範例程式碼

來源:互聯網
上載者:User

在做項目時,遇到了操作iframe的相關問題。業務很簡單,其實就是在操作iframe內部某個表單時,調用父表單的一個函數。於是就寫了兩個很簡單的htm頁面用來測試,使用網上流行的方法在Google瀏覽器中始終報錯,不能通過。
父頁面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="應該擷取的值" />rrr
</body>
</html>

網路上流行的方法 var t=window.parent; t.ParentFunction();在IE中能調用,可是在Google瀏覽器中總是提示如下錯誤,
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
網上找了很長時間都沒法探索方法,有的也是很早以前的版本,基本上沒用了,而且人云亦云,基本上沒有測試過。於是自己摸索,後來才發現,Google瀏覽器其實那種方法其實也可以,只是很奇怪,必鬚髮布後才可以,在檔案系統中調用,就會出現上邊的錯誤。
其實還有一種html5的方法postMessage,於是就根據著進行了改寫,最終代碼如下:
父頁面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="應該擷取的值" />rrr
</body>
</html>

經過改寫後,在檔案系統中雖然也會出現那個錯誤,但需要調用的方法確實調用了,目的確實達到了,不影響使用了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.