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


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 >绐楀彛鑷€傚簲---缁曞紑鍚屾簮绛栫暐鐨勯檺鍒讹紝鍚屾椂鍙堝埄鐢ㄥ悓婧愮瓥鐣ワ紝鍘绘帀iframe鐨勬粴鍔ㄦ潯锛屽姩鎬佽皟鏁寸獥鍙g殑楂樺害锛岃鍏惰兘澶熸樉绀鸿宓屽椤甸潰鐨勬墍鏈夊唴瀹 lt;/div> <!-- 闇€瑕佸姩鎬佽皟鏁撮珮搴︾殑iframe --> <div > <iframe name="domainB" src="" width="80%" scrolling="no" frameborder="0"$amp;>amp;$lt;/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> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body onclick="proxy()"> <!-- 琚獳鍩熸墍宓屽叆鐨勯〉闈 --> <button type="button" onclick="btnClick()">鍒囨崲鏄剧ず</button> <div > 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 鍦↗avaScript涓紝鏈変竴涓緢閲嶈鐨勫畨鍏ㄦ€ч檺鍒讹紝琚О涓衡€淪ame-Origin Policy鈥濓紙鍚屾簮绛栫暐锛夈€ 杩欎竴绛栫暐瀵逛簬JavaScript浠g爜鑳藉璁块棶鐨勯〉闈㈠唴瀹瑰仛浜嗗緢閲嶈鐨勯檺鍒讹紝鍗矹avaScript鍙兘璁块棶涓庡寘鍚畠鐨勬枃妗e湪鍚屼竴鍩熶笅鐨勫唴瀹广€ 鎵€璋撳悓婧愭槸鎸囷紝鍩熷悕锛屽崗璁紝绔彛鐩稿悓銆 </div> <div > 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ 姣斿涓€涓粦瀹㈢▼搴忥紝浠栧埄鐢↖Frame鎶婄湡姝g殑閾惰鐧诲綍椤甸潰宓屽埌浠栫殑椤甸潰涓婏紝褰撲綘浣跨敤鐪熷疄鐨勭敤鎴峰悕锛屽瘑鐮佺櫥褰曟椂锛 浠栫殑椤甸潰灏卞彲浠ラ€氳繃Javascript璇诲彇鍒颁綘鐨勮〃鍗曚腑input涓殑鍐呭锛岃繖鏍风敤鎴峰悕锛屽瘑鐮佸氨杞绘澗鍒版墜浜嗐€ </div> <!-- 鍙嶅悜宓屽叆A鍩熺殑椤甸潰 --> <iframe name="domainA" src="" $amp;>amp;$lt;/iframe> <!-- script --> <script type="text/javascript"> //闅愯棌鎴栨樉绀篸iv function btnClick() { var div = document.getElementsByTagName("div"); for(var i in div) { if(i<div.length) div[i].style.display = (div[i].style.display == 'none') "block" : "none"; } } </script> <script type="text/javascript"> function proxy(){ //alert("btn click"); //鑾峰彇鏈〉闈㈢殑楂樺害 var scrollHeight = document.documentElement.scrollHeight; //alert("婊氬姩鏉¢珮搴 "+scrollHeight); //灏嗙獥鍙g殑楂樺害鍏堣缃埌灞炰簬domainA鐨刬frame鐨剆rc灞炴€т笂 var iframeDomainA = document.getElementsByName("domainA")[0]; //鐢╠omainA鐨勪竴涓〉闈綔涓轰唬鐞嗭紝鏈€缁堢殑鐩殑鏄В鍐斥€滃悓婧愮瓥鐣モ€濈殑闄愬埗 var url = "http://localhost:9000/agent"; /**鎶€宸э細閫氳繃鏃堕棿鎴宠娴忚鍣ㄦ瘡娆¢噸鏂板姞杞絠frame锛岃繖鏍锋瘡娆¢兘浼氳嚜鍔ㄨ缃宓屽鐨刬frame鐨勯珮搴︼紝閬垮厤浜嗕娇鐢╯etInterval()*/ iframeDomainA.src = url+ " time=" + new Date().getTime() + "#" + scrollHeight ; } window.onload = proxy; </script> </body> </html>

  聽

  聽

java 程序开发 JavaScript

dcmz00 16 years, 4 months ago

Your Answer