一、刷新或重新加载当前页面

序号方法
1history.go(0)
2location.reload()
3location=location
4location.assign(location)
5document.execCommand(‘Refresh’)
6window.navigate(location)
7location.replace(location)
8document.URL=location.href

1、reload 方法

语法: location.reload([forceGet])
参数: forceGet, 可选参数, 默认为 false,从客户端缓存里取当前页。true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5(“刷新”)

2、 replace 方法

语法: location.replace(URL)
说明: 该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,你不能通过“前进”和“后退”来访问已经被替换的URL。 通常使用location.reload() 或者是 history.go(0) 来刷新当前页面,此方法类似点F5刷新,所以当method=”post”时,因为Session的安全保护机制,会出现“网页过期”的提示。

例: location.replace(location.href);
其中location.href为当前页面url。


二、返回并刷新前一个页面

window.open(document.referrer,"_parent",''); //已亲测,返回前一个页面并刷新

location.replace(document.referrer);

注:document.referrer 为前一个页面的URL。

返回不刷新前一个页面可以用:
history.go(-1);

history.back();


二、定时刷新(或跳转)页面

1、定时刷新当前页面

每隔3秒刷新一次页面:

<meta http-equiv=“refresh” content=“3”>

2、定时跳转

<meta http-equiv=“refresh” content=“2;url=‘https://www.baidu.com’”>

注:<和meta之间不能有空格。

3、其他方法

(1)延迟执行一次

setTimeout(code, milliseconds)
注: 使用 clearTimeout() 来停止 setTimeout() 的执行。

(2)定时执行

setInterval(code, milliseconds);
注: 使用 clearInterval() 来停止 setInterval 的执行。


三、刷新包含框架的页面

1、刷新包含该框架的页面

<script language=JavaScript>
    parent.location.reload();
</script> 

2、子窗口刷新父窗口

<script language=JavaScript>
    self.opener.location.reload();
</script> 

3、刷新另一个框架的页面

window.parent.frames[1].location.reload();
window.parent.frames.bottom.location.reload();
window.parent.frames[“bottom”].location.reload();
window.parent.frames.item(1).location.reload();
window.parent.frames.item(‘bottom’).location.reload();
window.parent.bottom.location.reload();
window.parent[‘bottom’].location.reload();

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐