怎么在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 οnlοad="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 οnlοad="onloadFun()">
<div style="border:#FF00ff 1px solid">
子窗口<br/>
<button οnclick="刷新顶级主窗体Fun()">刷新顶级主窗体</button><br/>
<button οnclick="刷新iframe子窗体Fun()">刷新iframe子窗体</button><br/>
<button οnclick="刷新父级窗体Fun()">刷新父级窗体</button><br/>
</div>
</body>
</html>
下面是运行效果:
首先是点击(刷新顶级主窗体)按钮效果:


点击(刷新iframe子窗体)按钮效果:

点击(刷新父级窗体)按钮效果:



这里要特别注意:location.href和parent.location.href在ie模式下可以在本地运行,在360浏览器下有可能会被阻止运行。不过在项目部署里面发布后不会被阻止。