日期:2014-05-16  浏览次数:20475 次

javascript鍚屾簮绛栫暐锛屽疄鐜癷frame绐楀彛楂樺害鑷€傚簲

domainA 涓湁涓€涓〉闈ndex.html锛岄€氳繃iframe宓屽浜哾omainB涓殑涓€涓〉闈ther.html

鐢变簬other.html椤甸潰鍦╥frame涓樉绀猴紝鑰屼笖鍏堕〉闈㈠唴瀹逛細鍔ㄦ€佺殑澧炲姞鎴栧噺灏戯紝鐜板湪闇€瑕佸幓鎺塱frame鐨勬粴鍔ㄦ潯

鐢变簬javascript鍚屾簮绛栫暐鐨勯檺鍒讹紝鏃犳硶杩涜璺ㄥ煙鎿嶄綔锛屼娇寰楅棶棰樻瘮杈冩鎵?/span>

鍙傝€冧簡涓€涓嬬綉涓婄殑鍋氭硶锛屽紩鍏ヤ簡涓€涓唬鐞嗛〉闈紝鎴栬€呭彨鍋氫腑浠?agent.html锛屽睘浜巇omainA

鐒跺悗锛屽湪domainB 涓殑other.html涓紝鍐嶄娇鐢╥frame灏哸gent.html杩涜宓屽

濂戒簡锛岀幇鍦ㄦ儏鍐垫槸杩欐牱鐨勶細

index.html 浣跨敤iframe 宓屽 other.html

other.html 浣跨敤iframe 宓屽 agent.html

涔嬫墍浠ヨ寮曞叆绗?涓〉闈gent.html锛屽氨鏄负浜嗛伒瀹堚€滃悓婧愮瓥鐣モ€濈殑瑙勫垯锛屽畬鎴愪笉鍚宒omain涓嬪弬鏁扮殑浼犻€掞紒聽

鎴戜滑鏈€缁堢殑鐩殑鏄鍘绘帀婊氬姩鏉★紝鍙堣淇濊瘉琚祵鍏ョ殑椤甸潰鍐呭鍏ㄩ儴寰楀埌鏄剧ず

1.鍙栧緱other.html椤甸潰鐨勫疄闄呴珮搴eight

2.灏唄eight璁剧疆鍒板叾宓屽叆鐨刬frame鐨剆rc灞炴€т笂

3.鍦╝gent.html涓埅鍙栧嚭鎵€灞瀒frame鐨剆rc灞炴€т腑鐨刪eight鍊?/span>

涓嬮潰鐨勪緥瀛愪腑锛屼娇鐢ㄤ簡涓€涓妧宸э紝閬垮厤浜嗕娇鐢╯etInterval()涓嶆柇鍘昏缃甶frame鐨勯珮搴?/span>

鍋氭硶鏄湪iframe鐨剆rc涓婏紝闄勫姞涓€涓椂闂存埑锛岃娴忚鍣ㄦ瘡娆¢兘閲嶆柊鍔犺浇agent.html

杩涜€岃agent.hml涓殑js鍑芥暟invokeMethodInTopWindow()寰楀埌鎵ц

domainA 涓殑2涓猦tml

index.html

#{extends 'main.html' /}
#{set title:'Home' /}

<hr>

<div style="color:red;font-weight:bold">绐楀彛鑷€傚簲---缁曞紑鍚屾簮绛栫暐鐨勯檺鍒讹紝鍚屾椂鍙堝埄鐢ㄥ悓婧愮瓥鐣ワ紝鍘绘帀iframe鐨勬粴鍔ㄦ潯锛屽姩鎬佽皟鏁寸獥鍙g殑楂樺害锛岃鍏惰兘澶熸樉绀鸿宓屽椤甸潰鐨勬墍鏈夊唴瀹?lt;/div>
<!-- 闇€瑕佸姩鎬佽皟鏁撮珮搴︾殑iframe -->
<div style="text-align:center;">
	<iframe name="domainB" src="http://127.0.0.1:8088/other" width="80%" scrolling="no" frameborder="0"></iframe>
</div>

<script type="text/javascript">
	function resize(height) {
		//alert("resize");
		document.getElementsByName("domainB")[0].height=height;
	}
</script>

agent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	鎴戞槸浠g悊椤甸潰鍝︼紒
<script type="text/javascript">
	window.onload = invokeMethodInTopWindow; 
	function invokeMethodInTopWindow() {
		//alert("璋冪敤鍚屽煙涓嬬殑鍑芥暟锛岄噸缃甶frame鐨勯珮搴?);
		var domainA = document.parentWindow;
		var realHeight = domainA.location.hash.split("#")[1];
		//last step:璋冪敤鏈€涓婂眰绐楀彛鐨勫嚱鏁帮紝閲嶇疆iframe鐨勯珮搴?
		parent.parent.resize(realHeight);

		//alert("realHeight:"+realHeight);
		//alert(document.parentWindow.name);//鑾峰彇瀹瑰櫒鎵€鍦ㄧ獥鍙g殑鍚嶇О domainA
		//error://alert(document.parentWindow.parent.name); //璁块棶澶辫触 锛氫笉鑳借闂甦omainB 
		//alert(document.parentWindow.parent.parent.name);//鏈€椤跺眰window灞炰簬domainA锛屽洜姝ゅ彲浠ヨ闂?
	}
	//浣跨敤涓嶅悓鐨勬椂闂存埑璁剧疆iframe鐨剆rc灞炴€у悗锛屽氨涓嶇敤浣跨敤setInterval()
	//setInterval("invokeMethodInTopWindow()",100);
</script>
</body>
</html>

domainB涓殑other.html

<!DOCTYPE html>
<html>
<head>
&l