麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-20 22:30:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在做項(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的代碼如下
復(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="測(cè)試" />
<iframe id="FRMdetail" name="FRMdetail" frameborder="0" src='child.html' style="width:100%;height:100%;" ></iframe>
</body>
</html>

子頁(yè)面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)用,可是在谷歌瀏覽器中總是提示如下錯(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的代碼如下
復(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() {//和注釋掉的方法是一樣的,也就是說(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的代碼如下
復(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)//在不支持時(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á)到了,不影響使用了。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 91精品福利视频 | 亚洲一级网站 | 国产小视频在线 | 久久国产不卡 | 国产精品久久久久久婷婷天堂 | 欧美性生交xxxxx免费观看 | 毛片免费一区二区三区 | 少妇一级淫片高潮流水电影 | 国产精品三级a三级三级午夜 | 日韩视频一区二区三区四区 | 久久国产精品电影 | 欧美精品一区二区中文字幕 | 精品中文字幕久久久久四十五十骆 | 免费黄色入口 | 国产一级毛片在线看 | 黄色成人小视频 | 五月天影院,久久综合, | 国产精品99久久久久久久女警 | 欧美成人免费在线视频 | 国产成人高清在线观看 | 欧美精品激情视频 | 97人操| 国产流白浆高潮在线观看 | 久久久久久久亚洲精品 | 久久免费视频3 | 美女久久久久久久久 | 久久久综合 | 亚州综合图片 | 国产电影av在线 | 国产91小视频在线观看 | 日本一区二区不卡高清 | 在线日韩av电影 | 欧美aⅴ视频 | 黄色网址电影 | 手机在线看片国产 | 一级免费特黄视频 | 国产一级免费不卡 | 黄色影院网站 | 精品在线观看一区 | 最新影院| av在线免费网 |