您的位置:首页 > Web前端 > JavaScript

js刷新页面方法大全

2013-06-16 11:59 330 查看
参考博客:/article/1260835.html

一、基本页面刷新方式:

1. history.go(0)

2. location.reload()

3. location=location

4. location.assign(location)

5. document.execCommand('Refresh')

6. window.navigate(location)

7. location.replace(location)

8. document.URL=location.href

二、框架之间刷新方式:

framedemo.html页面如下:

<HEAD>
<TITLE> frame </TITLE>
</HEAD>
<frameset rows="50%,50%">
<frame name=top src="top.html">
<frame name=bottom src="bottom.html">
</frameset>
</HTML>
如果在top页面里想要刷新bottom页面,就可以使用如下方式:

1. window.parent.frames[1].location.reload()

2. window.parent.frames.bottom.location.reload()

3. window.parent.frames['bottom'].location.reload()

4. window.parent.frames.item(1).location.reload()

5. window.parent.frames.item('bottom').location.reload()

6. window.parent.bottom.location.reload()

7. window.parent['bottom'].location.reload()

对以上做下解释:

1.window指代的是当前页面,例如对于此例它指的是top.html页面。

2.parent指的是当前页面的父页面,也就是包含它的框架页面。例如对于此例它指的是framedemo.html。

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

4.item是方法。返回数组里面的元素。

需要注意的是:

1.window.parent 是iframe页面调用父页面对象,而window.opener(或者 self.opener) 是 window.open 打开的子页面调用父页面对象;

2.如果子页面也是个框架页面,里面还是其它的子页面,那么上面的有些方法可能不行。

知道了这些基本的知识,就可以根据的自己的需要自由组合了,比如:

新窗口刷新父页面(比如window.open打开的),就可以用:self.opener.location.reload();

三、定时刷新方式:

1.页面自动刷新:把如下代码加入<head>区域中

<meta http-equiv="refresh" content="20">

其中20指每隔20秒刷新一次页面

2.页面自动跳转:把如下代码加入<head>区域中

<meta http-equiv="refresh" content="20;url=http://www.jb51.net">

其中20指隔20秒后跳转到http://www.jb51.net页面

3.页面自动刷新js版

<script language="JavaScript">

function myrefresh() {

window.location.reload();

}

setTimeout('myrefresh()',1000); //指定1秒刷新一次

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: