欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript父子頁(yè)面通訊實(shí)例詳解

 更新時(shí)間:2015年07月17日 15:22:34   作者:優(yōu)雅先生  
這篇文章主要介紹了javascript父子頁(yè)面通訊的實(shí)現(xiàn)方法,實(shí)例分析了javascript針對(duì)父子頁(yè)面通訊的原理與相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了javascript父子頁(yè)面通訊的實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:

如果一個(gè)domain為 www.abc.com的頁(yè)面內(nèi)部包含一個(gè)name屬性值為childFrame的iframe,并且這個(gè)iframe的domain為 static.abc.com。那么可以通過(guò)設(shè)置父頁(yè)面的domain為abc.com,子頁(yè)面的domain也為abc.com,然后實(shí)現(xiàn)父子頁(yè)面通訊(我這里有點(diǎn)混淆父子頁(yè)面和跨域的概念。

不采用上面的方法也是可以實(shí)現(xiàn)父子頁(yè)面相互訪問(wèn)的。
方法是:在父頁(yè)面用window.frames[0]或者window.frames["childFrame"],返回的是一個(gè)Window對(duì)象,然后就可以通過(guò):

var childWindow = window.frames[0];
// 或者 window.frames["childFrame"] 或者直接childFrame 或者childFrame.window 
var childDoc = childWindow.contentDocument || childWindow.document; 

利用childWindow可以訪問(wèn)執(zhí)行子頁(yè)面定義的函數(shù),利用childDoc可以訪問(wèn)子頁(yè)面的DOM節(jié)點(diǎn)。

而子頁(yè)面要訪問(wèn)父頁(yè)面,可以通過(guò)parent(Window對(duì)象),如果一個(gè)頁(yè)面已經(jīng)是頂級(jí)頁(yè)面那么parent==self將返回true:

if(parent != self) {
// 當(dāng)前頁(yè)面有父頁(yè)面 
  // 調(diào)用父頁(yè)面的函數(shù) 
  parent.parentFunc(); 
  var parentDoc = parent.contentDocument || parent.document; 
  // 訪問(wèn)父頁(yè)面的DOM節(jié)點(diǎn) 
}

www.abc.com父頁(yè)面:

document.domain = 'abc.com';
var ifr = document.createElement('iframe');
ifr.src = 'http://static.abc.com/';
ifr.style.display = 'none';
document.body.appendChild(ifr);
ifr.onload = function(){
  var doc = ifr.contentDocument || ifr.contentWindow.document;
  // 在這里操縱子頁(yè)面
  alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue);
};

www.static.abc.com子頁(yè)面:

復(fù)制代碼 代碼如下:
document.domain = 'abc.com';

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論