js刷新框架子页面的七种方法

JavaScript015

js刷新框架子页面的七种方法,第1张

下面以三个页面分别命名为l l l为例来具体说明如何做

其中l由上下两个页面组成 代码如下

<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN ><HTML><HEAD><TITLE>frameDemo </TITLE></HEAD><frameset rows= % % ><frame name=top src= l ><frame name=button src= l ></frameset></HTML>

现在假设l即上面的页面有一个button来实现对下面页面的刷新 可以用以下七种语句 哪个好用自己看着办了

语句 window parent frames[ ] location reload()

语句 window parent frames bottom location reload()

语句 window parent frames[ bottom ] location reload()

语句 windem( ) location reload()

语句 windem( bottom ) location reload()

语句 window parent bottom location reload()

语句 window parent[ bottom ] location reload()

解释一下

window指代的是当前页面 例如对于此例它指的是l页面

parent指的是当前页面的父页面 也就是包含它的框架页面 例如对于此例它指的是l

frames是window对象 是一个数组 代表着该框架内所有子页面

em是方法 返回数组里面的元素

如果子页面也是个框架页面 里面还是其它的子页面 那么上面的有些方法可能不行   l源代码 (页面上有七个按钮 功能都是刷新下面的框架页面)

<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN ><HTML><HEAD></HEAD><BODY><input type=button value= 刷新 onclick= window parent frames[ ] location reload() ><br><input type=button value= 刷新 onclick= window parent frames bottom location reload() ><br><input type=button value= 刷新 onclick= window parent frames[ bottom ] location reload() ><br><input type=button value= 刷新 onclick= windem( ) location reload() ><br><input type=button value= 刷新 onclick= windem( bottom ) location reload() ><br><input type=button value= 刷新 onclick= window parent bottom location reload() ><br><input type=button value= 刷新 onclick= window parent[ bottom ] location reload() ><br></BODY></HTML>

下面是l页面源代码 为了证明下方页面的确被刷新了 在装载完页面弹出一个对话框

lishixinzhi/Article/program/Java/Javascript/201311/25475

<iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe>

方案一:用iframe的name属性定位

<input type="button" name="Button" value="Button"

onclick="document.frames('ifrmname').location.reload()">

<input type="button" name="Button" value="Button"

onclick="document.all.ifrmname.document.location.reload()">

方案二:用iframe的id属性定位

<input type="button" name="Button" value="Button"

onclick="ifrmid.window.location.reload()">

终极方案:当iframe的src为其它网站地址(跨域操作时)

<input type="button" name="Button" value="Button"

onclick="window.open(document.all.ifrmname.src,'ifrmname','')">

同域下可以这样实现

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="Generator" content="EditPlus®">

<meta name="Author" content="">

<meta name="Keywords" content="">

<meta name="Description" content="">

<title>Document</title>

</head>

<body>

<iframe id="test" frameborder=0 width=250 height=250 marginheight=0 marginwidth=0 scrolling=no src="a.html"></iframe>

<script type="text/javascript">

// 兼容IE事件绑定

function addEvent(elem, eventName, func) {

if (!elem) return

if (window.addEventListener) {

elem.addEventListener(eventName, func, false)

} else if (window.attachEvent) {

elem.attachEvent('on' + eventName, func)

}

}

function onKeyUp(e) {

e = e || window.event

var iframe = document.getElementById('test')

var keyCode = e.keyCode

if (keyCode === 13) {

iframe.contentWindow.location.reload()

}

}

addEvent(document.body, 'keyup', onKeyUp)

</script>

</body>

</html>