最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js操作iframe兼容各種主流瀏覽器示例代碼

 更新時間:2013年07月22日 17:45:28   作者:  
遇到了操作iframe的相關問題,其實就是在操作iframe內(nèi)部某個窗體時,調(diào)用父窗體的一個函數(shù),下面與大家分享下操作iframe兼容各種瀏覽器的方法
在做項目時,遇到了操作iframe的相關問題。業(yè)務很簡單,其實就是在操作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="應該獲取的值" />rrr
</body>
</html>

網(wǎng)絡上流行的方法 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ù)著進行了改寫,最終代碼如下:
父頁面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>

經(jīng)過改寫后,在文件系統(tǒng)中雖然也會出現(xiàn)那個錯誤,但需要調(diào)用的方法確實調(diào)用了,目的確實達到了,不影響使用了。

相關文章

最新評論

新密市| 南丹县| 博客| 抚宁县| 梨树县| 太仆寺旗| 宝鸡市| 江门市| 漠河县| 小金县| 沂水县| 夏河县| 四会市| 榆林市| 三门县| 祁东县| 阿拉善右旗| 宁夏| 桃园市| 巴林右旗| 灵宝市| 县级市| 繁峙县| 宜兰县| 景宁| 鸡泽县| 拉萨市| 色达县| 乐陵市| 邢台市| 库伦旗| 赤城县| 察隅县| 团风县| 佳木斯市| 克什克腾旗| 灯塔市| 赤城县| 鹿邑县| 双城市| 丰都县|