iframe控制主視窗和子視窗的重新整理
大家好,這裡我將向大家講解怎麼在iframe視窗裡面控制他的父視窗,最上層視窗和本身視窗的頁面重新整理。
下面是兩種html的代碼
首先是主視窗.html========================================================= <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>iframe控制主視窗和子視窗重新整理--主視窗</title> <script type="text/javascript"> function onloadFun(){ alert("重新整理主視窗"); } </script> </head> <body onload="onloadFun()"> <div style="border:#FF0000 1px solid"> 主視窗 <iframe width="300px" height="300px" src="iframe子視窗.html" marginheight="0px" marginwidth="0px" frameborder="0" scrolling="no"></iframe> </div> </body> </html>
接下來是iframe子視窗.html==================================================== <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>iframe控制主視窗和子視窗重新整理--子視窗</title>
<script type="text/javascript"> function 重新整理頂級主表單Fun(){ top.location.href=top.location.href;//重新整理頂級頁面 } function 重新整理iframe子表單Fun(){ location.href=location.href; } function 重新整理父級表單Fun(){ parent.location.href=parent.location.href; alert("重新整理父視窗"); } function onloadFun(){ alert("重新整理子視窗"); } </script>
</head>
<body onload="onloadFun()"> <div style="border:#FF00ff 1px solid"> 子視窗<br/> <button onclick="重新整理頂級主表單Fun()">重新整理頂級主表單</button><br/> <button onclick="重新整理iframe子表單Fun()">重新整理iframe子表單</button><br/> <button onclick="重新整理父級表單Fun()">重新整理父級表單</button><br/> </div> </body> </html>
下面是運行效果: 首先是點擊(重新整理頂級主表單)按鈕效果: ——》
點擊(重新整理iframe子表單)按鈕效果:
點擊(重新整理父級表單)按鈕效果:
——》 ——》
這裡要特別注意:location.href和parent.location.href在ie模式下可以在本地運行,在360瀏覽器下有可能會被阻止運行。不過在項目部署裡面發布後不會被阻止。
文章來源: http://www.xingchen900.com/webProj/BaoXiaoWeb/WebMould/mould.html?htmlID=f763e62da83344495980cda8b6c1baf4b.html