DIV CSS 佈局教程網

兼容各大主流瀏覽器的 JS 操作 iframe 示例
編輯:關於JavaScript     

在做專案時,我們常需要在 iframe 內部調用父頁面的函數。這個看似簡單的需求,卻在跨瀏覽器時踩到了坑——特別是 Chrome 基於安全策略拋出的錯誤。本文記錄了完整的排查與解決過程。

最初的嘗試與困境

業務很簡單:在操作 iframe 內部窗體時,調用父窗體的一個函數。於是我寫了兩個簡單的 htm 頁面測試。父頁面 parent.html:

<html xmlns="https://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="https://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 中能調用,但在 Chrome 中卻總是報錯:Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match. 這是 Chrome 基於同源策略的安全限制。折騰了很久才發現,這種方法其實在 Chrome 也可以,只是很奇怪——必須「發布後」才能用,直接在文件系統(file://)中調用就會報上述錯誤。

解決方案:HTML5 的 postMessage

於是改用 HTML5 的 postMessage 進行跨框架通信。父頁面 parent.html:

<html xmlns="https://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="https://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>

方案小結

子頁面先判斷父頁面函數是否存在:若存在就直接調用(相容 IE),否則改用 postMessage 發送訊息(相容現代瀏覽器);父頁面則透過監聽 message 事件接收並處理。經過改寫後,即使在文件系統中偶爾仍會出現那個錯誤提示,但需要調用的方法確實執行了,目的已然達到,不影響使用。這個案例說明:面對跨瀏覽器相容問題,善用 HTML5 標準 API 往往比依賴舊有寫法更為可靠。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved