JavaScript页面刷新与跳转
1. JavaScript页面刷新
1.1 js刷新当前页面
1) history.go(0)
2)ocation.reload()
3)location=location
4 ) location.assign(location)
5)document.execCommand('Refresh')
6)location.replace(location)
7)document.URL=location.href
8)window.navigate(location)
1.2 js刷新包含该框架的页面
<script language="javascript">
parent.location.reload();
</script>
1.3 Asp.net输出刷新父窗口脚本
1.this.Response.write("<script>opener.location.reload();</script>");
2.Response.write("<script>opener.window.location.href = opener.window.location.href;</script>");
3.Response.Write("<script language=javascript>opener.window.navigate(''WebForm1.aspx'');</script>")
2. js实现页面跳转
2.1 window.location
<script language="javascript" type="text/javascript">
window.location="HTMLPage1.htm?backurl="+window.location.href;
</script>
<script language="javascript" type="text/javascript">
window.location.href="HTMLPage1.htm?backurl="+window.location.href;
</script>
window.location是页面的位置对象,window.location.href是 location的一个属性值,并且它是location的默认属性。对window.location直接赋值一个url实际上就是对window.location.href赋值。
2.2 window.top.location
<script language="javascript">alert("非法访问!");
window.top.location=' HTMLPage1.htm';
</script>
或
<script language="javascript">alert("非法访问!");
window.top.location.href =' HTMLPage1.htm ';
</script>
top表示最顶级的窗口,也就是最外层的窗口。如果一个大窗口中嵌套了几个小窗口,那么在小窗口中使用top就表示最外面的大窗口,就是这个意思了
2.3 window.self.location
<script language="JavaScript">
self.location='top.htm';
</script>
<script language="JavaScript">
window.self.location='top.htm';
</script>
2.4 window.history.back和window.history.go
在src.htm 通过<a href="HTML1.htm">跳转</a>,在HTML1.htm通过以下代码跳转回src.htm页面。
<script language="javascript">alert("返回");
window.history.back();
</script>
或返回并刷新
<script language="javascript">alert("返回");
window.history.back();
location.reload();
</script>
或返回上一页
<script language="javascript">alert("返回");
window.history.go(-1);
</script>
返回两个页面
<script language="javascript">alert("返回");
window.history.go(-2);
</script>
2.5 window.navigate
其他:(只针对IE)
<script language="javascript">
window.navigate("HTMLPage1.htm ");
/script>
说明:
location属性是每个浏览器都支持的。
window.navigate(sURL)方法是针对IE的,不适用于FF,在HTML DOM Window Object中,根本没有列出window.navigate方法。
3. 页面自动刷新、自动跳转
3.1页面自动刷新
把如下代码加入<head>区域中,每隔10秒刷新一次页面
<meta http-equiv="refresh" content="10">
3.2页面自动跳转
把如下代码加入<head>区域中,指隔5秒后跳转到HTMLPage2.htm页面
<meta http-equiv="refresh" content="5;url=HTMLPage2.htm">
3.3页面自动刷新js版
<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()',5000); //指定5秒刷新一次
</script>