做者 zhangljavascript
<!--父-->
<iframe src="./children.html" frameborder="1"></iframe>
<!--獲取子變量-->
<script> var cIframe = document.getElementsByTagName('iframe')[0]; cIframe.onload = function () { console.log(cIframe.contentWindow.childrenAge); }; </script>
複製代碼
<!--子-->
<!--定義變量-->
<script> var childrenAge = 20; </script>
複製代碼
<!--父-->
<iframe src="./children.html" frameborder="1"></iframe>
<!--定義變量-->
<script> var fatherAge = 50; </script>
複製代碼
<!--子-->
<!--獲取變量-->
<script> var fIframe = window.parent; console.log(fIframe.fatherAge); </script>
複製代碼
iframe(dom元素).onload = function () {}
複製代碼
iframe.onreadystatechange = function () {
if (iframe.readyState === 'complete' || iframe.readyState === 'loaded') {
alert('Local iframe is now loaded');
}
}
複製代碼
<!--origin http協議-->
<iframe src="./iframe2.html" frameborder="1"></iframe>
<script> var oIframe = document.getElementsByTagName('iframe')[0]; var oIframeSrc = oIframe.src; var age = 55; document.onclick = function () { oIframe.src = oIframeSrc + '#' + age; age++; }; </script>
複製代碼
<!--target file協議-->
<div>iframe2222</div>
<script> var lct = window.location; var preHash = lct.hash; setInterval(function () { if (preHash !== lct.hash) { console.log(lct.hash); preHash = lct.hash; } }, 300); </script>
複製代碼
觸發origin頁面點擊事件,查看target頁面的執行結果html
這裏須要藉助一個臨時頁面充當兩個跨域頁面的橋樑java
<!--target http協議-->
<iframe src="./iframe2.html" frameborder="1"></iframe>
<script> var oIframe = document.getElementsByTagName('iframe')[0]; var flag = true; oIframe.onload = function () { if (flag) { oIframe.src = './temp.html'; flag = false; } else { console.log(oIframe.contentWindow.name); } }; </script>
複製代碼
<!--tmep-->
空
複製代碼
<!--orgin file協議-->
<div>iframe2222</div>
<script> window.name = 20; </script>
複製代碼